Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Null Pointer Exception

Status
Not open for further replies.

Dustman

Programmer
May 7, 2001
320
US
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.
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
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
 
Hi,
If u see the working of the for loop, it works like this, the for loop executes while the condition u specify is true. but if u see in
&quot;for(tmp = 0; tmp >= level - 1; tmp++)&quot;

it will execute only once i.e for tmp = 0 because the next time it comes to the condition check, a false will be returned thats why only worm[0] is present and all other values for worm will be null.. i think u should change ur for loop to for(tmp = 0; tmp <= level - 1; tmp++) and check the working of ur program..

Hope this helps,
Reddy316
 
Arr.. I can't believe I missed that. I must have just hit the wrong key when I originally typed it. Its so frustrating to spend hours trying to fix something like that... I was about to go crazy. Thank you so much for pointing it out.. it works great now. -Dustin
Rom 8:28
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top