I've got the following global array...
public static float camObjCoord[] = new float[8000];
I've got a method feeding values into its indexes I'm sending them like so...
layers.addcube(-6, -2, -6, -2);
i is a class variable declared as..
int i = 0;
and layers is declared as...
GLLayer layers = new GLLayer(this);
But when I do I get a NullPointerException at....
public void addcube(float highx, float lowx, float highz, float lowz){
//Constructing new cube...
cubes++;
float highy = 4.5f;
fl开发者_StackOverflow社区oat lowy = 2.5f;
//FRONT
Global.camObjCoord[i] = highx; //null pointer here.
Does anyone have any idea why it's giving me a null pointer? There is more code but I thought it'd be sufficent to give you only what's relevant.
Thanks
Try changing it to this:
public final static float camObjCoord[] = new float[8000];
As I assume you somwehere update camObjCoord and set it to null. Otherwise I do not see how you could get a NullPointerException in that line.
Are you sure this is a NullPointerException and not an ArrayIndexOutOfBoundsException? If start != 0
, then i is the length of the array. However, array indices run from 0 through to length - 1
- so you need i = Global.camObjCoord.length - 1;
to get a valid index in the line you claim throws the exception.
The highest array index should be array.length - 1
. So
if(start != 0){
i = Global.camObjCoord.length - 1;
}
Note that this line can throw an exception if it is executed before the value of camObjCoord
is assigned. It's pretty hard to force this situation, but it's possible (for example if a previous initializer contains a method call and you access camObjCoord
inside that method):
public class NPEThrower {
Object x = initX();
float[] foo = new float[8000];
private Object initX() {
int l = foo.length;
return "x";
}
}
Note that adding final
to the field definition doesn't solve this (which means that you can observe 2 different values for a final
field during execution: null
and the actual value!).
Given the code you're showing, there can't be a NullPointerException. Either the exception occurs at a diffetent line, or it's a different exception, or you're setting the array to null somewhere else.
You're assigning i = array.length
and then later assigning to the element at i. This will access off the end of the array.
You should post a Stack trace, because I reckon it is not a NullPointerException
but an ArrayIndexOutOfBoundsException
. If you set i = Global.camObjCoord.length
then you can't index the array using i
since it will be off the end of the array. Think about it, if you have an array of size 1, then the only index you can use is 0, if you try to access element 1 you will be off the end of the array and hence get an exception.
Like inflagranti already pointed out - the only way to get a real NPE here is to have camObjCoord
set to null
This is a way to test it:
// DEBUG Code - start
if (camObjCoord == null) {
System.out.println("Who the F*** played with comObjCoord! NPE comeing next.");
}
if (i >= camObjCoord.length) {
System.out.println("There's nothing outside the array. AIOOBE thrown ... now!");
}
// DEBUG Code - end
Global.camObjCoord[i] = highx; //null pointer here.
I think that when you use i = Global.camObjCoord.length;
, it sets i = 8000
, so when you try to push your value, it puts it on the index 8000, which is not possible for a table going from 0 to 7999.
Try this to be sure:
//FRONT
System.out.println('Value of index i : '+i);
Global.camObjCoord[i] = highx; //null pointer here.
Then, you should see the value of i, and see if it goes beyond array boundaries.
Hope I have helped.
精彩评论