1

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;
    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

kenju
  • 5,866
  • 1
  • 41
  • 41
Skizit
  • 43,506
  • 91
  • 209
  • 269
  • 2
    Post a stacktrace you're getting. – Peter Štibraný Jul 12 '10 at 13:20
  • 1
    What's the value of `i` (and by extension `Global.camObjCoord.length`)? – ChrisF Jul 12 '10 at 13:20
  • @ ChrisF If you look in the if / else `i` is declared as `0` or the end of `Global.camObjCoord` depending on the condition – Skizit Jul 12 '10 at 13:21
  • what's this Global? It looks like it's null. What's the value of start? – Paulo Guedes Jul 12 '10 at 13:27
  • "There is more code but I thought it'd be sufficent to give you only what's relevant." That is always hard to judge. If you knew what's relevant (i.e. where exactly the problem is) you probably would already have found it. If it is indeed a NullPointerException, then I suspect you change camObjCoord somewhere in the code you didn't post. – Janick Bernet Jul 12 '10 at 13:45

9 Answers9

3

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
  • 48,938
  • 12
  • 131
  • 152
3

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.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55
  • 1
    @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." – Janick Bernet Jul 12 '10 at 13:28
  • 2
    @DaveJohnston - maybe because you can't set camObjCoord to `null` later because it's final? – Andreas Dolk Jul 12 '10 at 13:29
  • I posted my comment before you updated your answer to include the explanation, fair enough. – DaveJohnston Jul 12 '10 at 13:31
  • Looks like the edit history is not complete ;) Anyway - if it's a NPE, than I don't see any other reason too. – Andreas Dolk Jul 12 '10 at 13:34
  • @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. – Janick Bernet Jul 12 '10 at 13:36
2

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

if(start != 0){
        i = Global.camObjCoord.length - 1;
    } 
kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
2

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
  • 302,674
  • 57
  • 556
  • 614
1

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
  • 342,105
  • 78
  • 482
  • 720
0

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
  • 3,563
  • 16
  • 16
0

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.

3rgo
  • 3,115
  • 7
  • 31
  • 44
0

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
  • 10,031
  • 10
  • 54
  • 83
0

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 Dolk
  • 113,398
  • 19
  • 180
  • 268