HasMap NullPointerException

I have implemented a HashMap in the following way:


HashMap<Long,ArrayList<MouseEnterListener>> map = new HashMap<>();
map.put( id,new ArrayList<MouseEnterListener>());

When trying to access the map, I get a NullPointerException:


for( MouseEnterListener mouseEnterListener: map.get( id ) ) // Exception here
	mouseEnterListener.invoke( entered );

What I’ve debugged sofar:

  • “id” is correct and the HashMap contains an entry for this id
  • the size of the “map” is 1, while the ArrayList is empty

What’s going wrong here?

First, please provide a detailed exception.
Second, the usual way how this should be done is to first check whether the map really contains the id or not. If it does contain, then iterate, else handle it.

Okay, just to clarify, I do that, but I didn’t want to spam code.
And the exception would be:
Exception in thread “main” java.lang.NullPointerException

Please add whole code and/or stacktrace and I can take a look at it:)
Is id null?

Alright, whole code.
In the class:

private static HashMap<Long,ArrayList<MouseEnterListener>> map = new HashMap<>();

Check if id is already contained, if not, add list for it:

if( !map.containsKey( id ) ) {
	map.put( id,new ArrayList<MouseEnterListener>() );
}

Accessing the HashMap:

for( MouseEnterListener mouseEnterListener : map.get( id ) ) // line 235
	mouseEnterListener.invoke( entered );

The exact exception:

Exception in thread "main" java.lang.NullPointerException
	at glfw4j.input.Mouse.lambda$register$3(Mouse.java:235)
	at ... (unimportant classes)

You’re not ever putting anything into “map”. It looks you are checking whether or not “map” has the id but then putting the new Array List into a separate HashMap called mouseEnterListeners. But then you continue using “map”
edit:
so to fix it change to:


if( !map.containsKey( id) ) {
   map.put( glfwWindowID,new ArrayList<MouseEnterListener>() );
}

My mistake, got some field names mixed up… the correct code is now up. This is how it’s actually in my code.

Have you tried stepping through and debugging it?

I’ve debugged all points, everything should be working. The id is the same everywhere in the program. There is an ArrayList in the corresponding place in the HashMap. Everything works, right until the for-loop get’s executed and “map.get( id )” get’s executed.

Still not enough info.
Is glfw4j.input.Mouse your class or external?
Hard to say anything if you not paste whole code.

If you get into trouble like this just check what is actually null and causing nullpointer. Use debugger.

Which one of these is null?

The code seems to work fine in isolation:
http://tpcg.io/6EtDDs

Might need to see more to help further.

Yes, it’s my internal class. And the ArrayList seems to be null, even though I added one and the HashMap says it has a size of 1.
Here is the entirety (it’s a lot, that’s why I didn’t want to post it initially); http://pastebin.java-gaming.org/4afa860655017

You want to get by key glfwWindow or glfwWindowId from the map?

glfwWindowID, as it’s the currently “active” window.

So you want to:
for( MouseEnterListener mouseEnterListener : mouseEnterListeners.get( glfwWindowID ) )
not
for( MouseEnterListener mouseEnterListener : mouseEnterListeners.get( glfwWindow ) )

Or I am missing something?
Remote debugging ftw xD

Thank you! That’s what you need a second pair of eyes for, after your own are square already from searching through your code.

Np :slight_smile: