Null pointer where there should not be one...at all

I have a null pointer error happening on this section of code


	public static Tile getTileByID(int i) {
		for (Tile t : tiles) {
			if ((byte) i == t.getID()) {
				return t;
			}
		}
		return Tile.VOID;
	}

//this is the problem line 
if ((byte) i == t.getID()) {

At first i thought it could be due to some data being over writen due to synchronization issues but this is not the case since i have a read right lock on both threads… any ideas?

Ohh and ‘Tiles’ never gets any new data written to it since its just a list of tiles creates on startup

public static Tile getTileByID(int i) {
      for (Tile t : tiles) {
         System.out.println(t);
         if ((byte) i == t.getID()) {
            return t;
         }
      }
      return Tile.VOID;
}

I’ll bet that prints null at some point.
Either that or the stacktrace is glitched and it’s actually the line above and tiles is null, although that is much less likely.

why are you certain that there shouldnt be a null point ? at least one time an object from that list is null
being in the list doesnt mean automatically that its not null

First off, why are you casting to a byte, why not just pass a byte to it in the first place?

Secondly, it’s pretty obvious that an object in your tile list is null some how, and with just the code you provided it’s impossible to be able to help you honestly.

Just printed it all out and no null, but the error in the stack-trace is still a null pointer on the next line after the print, so i checked to see if the ‘i’ variable was being set properly and it is

Well then step through it with a debugger and find out what variable is null at that time.

Tile.getID() does refer to an actual int doesn’t it… not a possibly unboxed Integer?

Cas :slight_smile:

There was no vairiable that was null, trust me i looked through everything, i switched over the way i saved all my tiles from

public static final Tile[] tiles = new Tile[256];

to

private static HashMap<Integer, Tile> tiles = new HashMap<Integer,Tile>();

and it works with no issues now, but i wrapped the whole statement in a try catch looking for null pointers and that also fixed it xD …it never throws a error once i did that

So uninitialized values in the array then.
What is the actual code that initializes or populates the values? That’s what matters.

It’s much easier to answer questions like these if you post an MCVE along with the question. Cut out all the guessing.

You mean silently catching and ignoring nullpointer exceptions ? That is… a really bad practice.

To debug exceptions, just set an exception break point in the IDE and wait until it gets triggered.

Nothing was ever being caught when i wrapped it so that’s why im getting confused

Have a look at that again. It’s got to be unboxing of a null value.

t.getID() returns null , at some point , just put an if(t != null){ check

I did, it did not work

So are we gonna be able to see some of the surrounding code or what? It’s all speculation until that happens.

You surely mean check if (t.getID() != null) { … }
assuming .getID() returns an java.lang.Integer (or a type assignable to it) instead of the primitive int.
So a question to the original poster: What type does Tile.getID() have???

Tile.class

http://pastebin.com/6bibJskA

Basic Tile class

http://pastebin.com/ZJ1mdrVe

Merge Tile class

http://pastebin.com/WfSbqrJA

So, if t absolutely, a-hundred-percently is not null never-ever, then your source files are out-of-sync with the compiled .class files and the NPE is actually happening somewhere else, but your IDE thinks it is there, because of messed-up linenumber attributes…

That’s the tile class that is fixed, right? That doesn’t help us figure out why the old one was broken. If it’s fixed now though, then it’s fixed.