
-----------------------------------
Fasih
Tue Apr 26, 2011 7:28 pm

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.
            }

        }
    }[/code]
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.

-----------------------------------
RandomLetters
Tue Apr 26, 2011 8:47 pm

RE:Null Pointer Exception...argh!
-----------------------------------
getWidth(),getHeight()

Don't you need an object there?

-----------------------------------
Zren
Tue Apr 26, 2011 8:53 pm

RE:Null Pointer Exception...argh!
-----------------------------------
Did you initiate the object?


Object obj =  new Object();

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.


Object

-----------------------------------
Fasih
Tue Apr 26, 2011 11:43 pm

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];[/code]
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);
        }[/code]

-----------------------------------
Fasih
Tue Apr 26, 2011 11:45 pm

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
Tue Apr 26, 2011 11:51 pm

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);
}[/code]

Think carefully about what's happening here.

-----------------------------------
Fasih
Tue Apr 26, 2011 11:57 pm

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
Wed Apr 27, 2011 12:08 am

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.

-----------------------------------
Fasih
Wed Apr 27, 2011 12:15 am

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
Wed Apr 27, 2011 12:45 am

RE:Null Pointer Exception...argh!
-----------------------------------
How bout a stack trace?

-----------------------------------
Zren
Wed Apr 27, 2011 1:14 am

RE:Null Pointer Exception...argh!
-----------------------------------
You declare the array AL at the start of the class.

public Ball

Then locally override the variable's name due to scope of the function.


public BallInBox (int num) {
Ball

Then in another function, your calling the array that is at class level, which has not been initialized.


public void actionPerformed (ActionEvent e) {
for (..._) AL 

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: 


class Meh {
    private boolean bleh = false;
    
    public void setBleh(boolean bleh) {
        this.bleh = bleh;
    }
}

-----------------------------------
Fasih
Wed Apr 27, 2011 8:52 am

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
Wed Apr 27, 2011 1:17 pm

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];[/code]
I should have been doing [code]AL = new Ball[num];[/code]
