views:

186

answers:

9

Hey Guys,

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 null pointer exception at....

public void addcube(float highx, float lowx, float highz, float lowz){
    //Constructing new cube...

    cubes++;
    float highy = 4.5f;
    float 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

+3  A: 

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.

Adam Wright
+2  A: 

The highest array index should be array.length - 1. So

if(start != 0){
        i = Global.camObjCoord.length - 1;
    } 
kgiannakakis
A: 

You're assigning i = array.length and then later assigning to the element at i. This will access off the end of the array.

bshields
A: 

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. Regards from France ;)

Squ36
+3  A: 

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.

inflagranti
Why would declaring it final resolve this problem?
DaveJohnston
@DaveJohnston: I quote: "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 code."
inflagranti
@DaveJohnston - maybe because you can't set camObjCoord to `null` later because it's final?
Andreas_D
I posted my comment before you updated your answer to include the explanation, fair enough.
DaveJohnston
Looks like the edit history is not complete ;) Anyway - if it's a NPE, than I don't see any other reason too.
Andreas_D
@DaveJohnston: Yeah, I'm aware its not your question, I quoted what I wrote to the original poster. And yes, I didn't have that explanation from the begining, so it was fair enough of you to ask for clarificaton.
inflagranti
+1  A: 

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.

Michael Borgwardt
A: 

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.

DaveJohnston
+2  A: 

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!).

Joachim Sauer
A: 

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.
Andreas_D