Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

So, I am having this bizarre error while initializing an array. What happens, is that I create an array, and it will throw java.lang.NullPointerException. As you can see on the second line, I do it correctly (In this text example, data.length / 4 equals 1):

short[] data = ByteToShort(Gdx.files.internal(file).readBytes());
O[ID].code.MapFile = new short[data.length / 4][4];

And this is what O[ID].code.MapFile looks like:

public short[][] MapFile;

And in case you need it, here is the stack dump:

Exception in thread "LWJGL Application" java.lang.NullPointerException
    at com.GS.SE.SonicEngine.LoadMappings(SonicEngine.java:288)
    at com.GS.SE.obj.ObjSonic.init(ObjSonic.java:47)
    at com.GS.SE.obj.Obj.<init>(Obj.java:8)
    at com.GS.SE.SonicEngine.create(SonicEngine.java:41)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)

I am completely stuck with this, it seems there no way to go around this or anything. Any help would be appreciated! If you need more info, comment below and I'll add.

EDIT: As commenters asked, the variables O[ID] and O[ID].code are NOT null.

EDIT 2: Even AFTER initializing O[ID].code.MapFile it WILL cause java.lang.NullPointerException.

EDIT 3: I figured out the issue. And it was something I didnt think could matter. As I run objects code threaded, it gets reference a lot there, and for some weird reason, I could not access any of the data from the init code which was ran on the main thread, however making the init code run on the object code thread, it made it work perfectly. Thanks for help anyway!

share|improve this question
2  
To which line does SonicEngine.java:288 refer? –  Emrakul Aug 7 '14 at 4:14
    
"As you can see on the second line, I do it correctly". With that I meant the line: O[ID].code.MapFile = new short[data.length / 4][4]; –  Green Snake Aug 7 '14 at 4:19
    
I'm not sure that answers my question... do you know what's on line 288 of SonicEngine.java? –  Emrakul Aug 7 '14 at 4:19
1  
It isn't clear what is null from just these two lines, so I would suggest checking the variables in a debugger or print them out to check if they are null. Things to print would be O[ID], O[ID].code, and data. –  nmore Aug 7 '14 at 4:27
1  
So if you are sure O[ID], O[ID].code are not null (as per your edit), then data is probably null. –  nmore Aug 7 '14 at 4:30

1 Answer 1

Based on your question and code, I assume that this

O[ID].code.MapFile = new short[data.length / 4][4];

is throwing your NullPointerException. There are two reasons it might, O[ID] is null. You didn't tell us what O is. It's also possible that the code field is null. Let's check,

if (O[ID] != null) {
  if (O[ID].code != null) {
    O[ID].code.MapFile = new short[data.length / 4][4];
  } else {
    System.out.println("O[ID].code == null");
  }
} else {
  System.out.println("O[ID] == null");
}

One possible solution might be

if (O[ID].code == null) {
  O[ID].code = new Code(); // <-- you didn't tell us.
}
// Then you could
O[ID].code.MapFile = new short[data.length / 4][4];
share|improve this answer
    
It is actually not a null, it is defined earlier on the program (and the thing is actually running the code): O[0] = new Obj(new ObjSonic(), 0); I also have made 100% sure it will work properly and not cause issues. Also the ID in case is 0 –  Green Snake Aug 7 '14 at 4:24
    
@GreenSnake Try my code above, it won't throw NullPointerException (unless of course it's data that is null). Then consider (again) what might be null when you get a NullPointerException. –  Elliott Frisch Aug 7 '14 at 4:28
    
I tested whether O[ID].code is null or not, and as I suspected, it turns it is not –  Green Snake Aug 7 '14 at 4:31
    
@GreenSnake What is line 288 of SonicEngine.java? The only other possibility is data being null. –  Elliott Frisch Aug 7 '14 at 4:43
    
O[ID].code.MapFile = new short[data.length / 4][4]; And also data is not null. –  Green Snake Aug 7 '14 at 4:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.