Okay.. I'm pretty new to java and I'm trying to write a simple program that draws several circles on the screen when loaded. I made a class called WormCircle that holds all the cordinates and a few other values for each one. The parent class is Shape. Everything compiles right but when I run it, I get a null pointer exception from my paint method.
I'm pretty sure everything is set up right. I just keep getting the null pointer exception when I try to run. Does the paint method automaticly run when I add a new item to my array? If so.. how do I keep it from painting untill the array is full? -Dustin
Rom 8:28
Code:
//WormAttack.java
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
public class WormAttack extends JApplet {
//Allows applet to be ran as an application.
public static void main(String[] args) {
JFrame f = new JFrame("Worm Attack");
WormAttack game = new WormAttack();
game.init();
f.getContentPane().add(game,"Center");
f.setSize(640,480);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
/****Variables****/
public int level;
public WormCircle[] worm;
/****Methods*****/
public void init()
{
setupGame(5);
}
public void setupGame(int lev)
{
level = lev;
int tmp;
int newX = 640;
worm = new WormCircle[level];
for(tmp = 0; tmp >= level - 1; tmp++)
{
worm[tmp] = new WormCircle(newX,0);
newX -= 15;
}
}
public void paint(Graphics g)
{
for(int x = 0;x<worm.length;x++)
{
worm[x].draw(g);
}
}
}//End of WormAttack.java
Rom 8:28