Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Null Pointer Exception...argh!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic
Author Message
Fasih




PostPosted: Tue Apr 26, 2011 7:28 pm   Post subject: Null Pointer Exception...argh!

So i have a program that basically animates cells. I have them set at a specific number, and i have a gui. When i added a custom cell option, it throws me a nullpointerexception.

Here is the code where the null pointer exception comes from:
note i am using a timer to do the animation, but thats not really important here

code:

    class TimerAction implements ActionListener
    {
        //================================================== actionPerformed
        /* ActionListener of the timer.  Each time this is called,
           the ball's position is updated, creating the appearance of
           movement.
         */
        public void actionPerformed (ActionEvent e)
        {
            System.out.println ("Timer check:" + numcells);
            for (int x = 0 ; x < numcells ; x++)
            {
                AL [x].setBounds (getWidth (), getHeight ());
                AL [x].move (); // Move the ball.

                repaint ();     // Repaint indirectly calls paintComponent.
            }
            for (int x = 0 ; x < WB.length ; x++)
            {
                WB [x].setBounds (getWidth (), getHeight ());
                WB [x].move (); // Move the ball.

                repaint ();     // Repaint indirectly calls paintComponent.
            }
            for (int x = 0 ; x < VC.length ; x++)
            {
                VC [x].setBounds (getWidth (), getHeight ());
                VC [x].move (); // Move the ball.

                repaint ();     // Repaint indirectly calls paintComponent.
            }

        }
    }

numcells is the size of the red blood cells array. The red blood cells is the only thing at the moment that the user can change, and the array name is AL. At the line AL[x].setBounds(getWidth(),getHeight()); is where it throws it. the array is being initialized, and the values in the array are being inputted.
 
Sponsor
Sponsor
Sponsor
sponsor
RandomLetters




PostPosted: Tue Apr 26, 2011 8:47 pm   Post subject: RE:Null Pointer Exception...argh!

getWidth(),getHeight()

Don't you need an object there?
 
Zren




PostPosted: Tue Apr 26, 2011 8:53 pm   Post subject: RE:Null Pointer Exception...argh!

Did you initiate the object?

Java:

Object obj =  new Object();

Object[] objArr = new Object[10];
for (Object o : objArr) o = new Object();


An array is a bunch of pointers to an object, and it doesn't initiate all the objects it points to when you declare it.

Java:

Object[] objArr; // Declare array
objArr = new Object[10]; // Initiate array (which is like declaring 10 new objects).
for (Object o : objArr) o = new Object(); // Initiate each object in the array.
 
Fasih




PostPosted: Tue Apr 26, 2011 11:43 pm   Post subject: RE:Null Pointer Exception...argh!

Yeah i declared it initially at the start of the class
code:
    public Ball[] AL = new Ball [5];
    public Ball[] WB = new Ball [3];
    public Ball[] VC = new Ball [2];

and then again in the constructor where the user puts in their own value:
code:
public BallInBox (int num)
    {
        setPreferredSize (new Dimension (300, 300));
        numcells = num;
        Ball[] AL = new Ball [num];
        System.out.print (AL.length);
        setBorder (BorderFactory.createLineBorder (Color.BLACK));
        m_timer = new Timer (m_interval, new TimerAction ());
        for (int x = 0 ; x < num ; x++)
        {
            AL [x] = new Ball ((int) (Math.random () * 250 + 1), (int) (Math.random () * 250 + 1), (int) (Math.random () * 3 + 1), (int) (Math.random () * 2 + 1), 1);
        }
 
Fasih




PostPosted: Tue Apr 26, 2011 11:45 pm   Post subject: RE:Null Pointer Exception...argh!

@RandomLetters
No i dont because its just getting the params so i can make the ball bounce off the sides
 
chrisbrown




PostPosted: Tue Apr 26, 2011 11:51 pm   Post subject: Re: RE:Null Pointer Exception...argh!

code:
m_timer = new Timer (m_interval, new TimerAction ());
for (int x = 0 ; x < num ; x++)
{
    AL [x] = new Ball ((int) (Math.random () * 250 + 1), (int) (Math.random () * 250 + 1), (int) (Math.random () * 3 + 1), (int) (Math.random () * 2 + 1), 1);
}


Think carefully about what's happening here.
 
Fasih




PostPosted: Tue Apr 26, 2011 11:57 pm   Post subject: RE:Null Pointer Exception...argh!

The same thing happens for the default set code, theres no error... the only thing different in this is the in the loop, insteat of AL.length i put the number the user inputs "num"
 
chrisbrown




PostPosted: Wed Apr 27, 2011 12:08 am   Post subject: RE:Null Pointer Exception...argh!

What I mean is, what are the consequences of creating a new TimerAction before initializing the elements of AL?

Actually, I may have misread your code. I'm just wondering if it's possible for actionPerformed to be called before the array is initialized.
 
Sponsor
Sponsor
Sponsor
sponsor
Fasih




PostPosted: Wed Apr 27, 2011 12:15 am   Post subject: RE:Null Pointer Exception...argh!

I tried moving and no luck so i dont think thats it. I would post my code, but its 500+ lines long
 
chrisbrown




PostPosted: Wed Apr 27, 2011 12:45 am   Post subject: RE:Null Pointer Exception...argh!

How bout a stack trace?
 
Zren




PostPosted: Wed Apr 27, 2011 1:14 am   Post subject: RE:Null Pointer Exception...argh!

You declare the array AL at the start of the class.

public Ball[] AL = new Ball [5];

Then locally override the variable's name due to scope of the function.

Java:

public BallInBox (int num) {
Ball[] AL = new Ball [num];
// initialize the contents of the local array.
} // The temporary array overriding the class level variable AL is forgotten.


Then in another function, your calling the array that is at class level, which has not been initialized.

Java:

public void actionPerformed (ActionEvent e) {
for (..._) AL [x].move (); //etc.
}


PS: Once you figure out your mistake, you can do funky stuff with the "this." to select the current class instances variables. (Sorta of a bad explanation) Eg:

Java:

class Meh {
    private boolean bleh = false;
   
    public void setBleh(boolean bleh) {
        this.bleh = bleh;
    }
}
 
Fasih




PostPosted: Wed Apr 27, 2011 8:52 am   Post subject: RE:Null Pointer Exception...argh!

Ohh right... but what about using a setter?

So I guess that if the user wants to input a custom amount of cells, then I would have to join the code together into one method since it goes back to the array size of 5 and never gets initialized :S
 
Fasih




PostPosted: Wed Apr 27, 2011 1:17 pm   Post subject: RE:Null Pointer Exception...argh!

Oh i figured it out. I was declaring a local variable inside the class,
code:
Ball[] AL = new Ball[num];

I should have been doing
code:
AL = new Ball[num];
 
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh pageAdd this topic to your bookmarks (CTRL-D) View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: