Computer Science Canada Null Pointer Exception...argh! |
Author: | Fasih [ 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
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. |
Author: | RandomLetters [ Tue Apr 26, 2011 8:47 pm ] |
Post subject: | RE:Null Pointer Exception...argh! |
getWidth(),getHeight() Don't you need an object there? |
Author: | Zren [ Tue Apr 26, 2011 8:53 pm ] | ||||
Post subject: | RE:Null Pointer Exception...argh! | ||||
Did you initiate the 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.
|
Author: | Fasih [ 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
and then again in the constructor where the user puts in their own value:
|
Author: | Fasih [ 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 |
Author: | chrisbrown [ Tue Apr 26, 2011 11:51 pm ] | ||
Post subject: | Re: RE:Null Pointer Exception...argh! | ||
Think carefully about what's happening here. |
Author: | Fasih [ 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" |
Author: | chrisbrown [ 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. |
Author: | Fasih [ 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 |
Author: | chrisbrown [ Wed Apr 27, 2011 12:45 am ] |
Post subject: | RE:Null Pointer Exception...argh! |
How bout a stack trace? |
Author: | Zren [ 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.
Then in another function, your calling the array that is at class level, which has not been initialized.
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:
|
Author: | Fasih [ 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 |
Author: | Fasih [ 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,
I should have been doing
|