Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (763)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (852)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  HasMap NullPointerException  (Read 873 times)
0 Members and 1 Guest are viewing this topic.
Offline DesertCoockie
« Posted 2018-05-13 17:08:48 »

I have implemented a HashMap in the following way:
1  
2  
HashMap<Long,ArrayList<MouseEnterListener>> map = new HashMap<>();
map.put( id,new ArrayList<MouseEnterListener>());

When trying to access the map, I get a NullPointerException:
1  
2  
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?
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #1 - Posted 2018-05-13 17:20:13 »

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.
Offline DesertCoockie
« Reply #2 - Posted 2018-05-13 17:22:58 »

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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ByerN
« Reply #3 - Posted 2018-05-13 17:28:22 »

Please add whole code and/or stacktrace and I can take a look at it:)
Is id null?
Offline DesertCoockie
« Reply #4 - Posted 2018-05-13 17:32:57 »

Alright, whole code.
In the class:
1  
private static HashMap<Long,ArrayList<MouseEnterListener>> map = new HashMap<>();

Check if id is already contained, if not, add list for it:
1  
2  
3  
if( !map.containsKey( id ) ) {
   map.put( id,new ArrayList<MouseEnterListener>() );
}

Accessing the HashMap:
1  
2  
for( MouseEnterListener mouseEnterListener : map.get( id ) ) // line 235
   mouseEnterListener.invoke( entered );

The exact exception:
1  
2  
3  
Exception in thread "main" java.lang.NullPointerException
   at glfw4j.input.Mouse.lambda$register$3(Mouse.java:235)
   at ... (unimportant classes)
Offline kingroka123
« Reply #5 - Posted 2018-05-13 17:39:38 »

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:
1  
2  
3  
if( !map.containsKey( id) ) {
   map.put( glfwWindowID,new ArrayList<MouseEnterListener>() );
}
Offline DesertCoockie
« Reply #6 - Posted 2018-05-13 17:45:49 »

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

Junior Newbie





« Reply #7 - Posted 2018-05-13 17:53:37 »

Have you tried stepping through and debugging it?
Offline DesertCoockie
« Reply #8 - Posted 2018-05-13 17:56:29 »

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.
Offline ByerN
« Reply #9 - Posted 2018-05-13 18:07:32 »

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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ohyeahx10

Junior Newbie





« Reply #10 - Posted 2018-05-13 18:08:47 »

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

Might need to see more to help further.
Offline DesertCoockie
« Reply #11 - Posted 2018-05-13 18:23:55 »

Is glfw4j.input.Mouse your class or external?

Which one of these is null?
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
Offline ByerN
« Reply #12 - Posted 2018-05-13 18:56:11 »

You want to get by key glfwWindow or glfwWindowId from the map?
Offline DesertCoockie
« Reply #13 - Posted 2018-05-13 19:15:48 »

glfwWindowID, as it's the currently "active" window.
Offline ByerN
« Reply #14 - Posted 2018-05-13 19:25:55 »

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
Offline DesertCoockie
« Reply #15 - Posted 2018-05-13 19:32:51 »

Thank you! That's what you need a second pair of eyes for, after your own are square already from searching through your code.
Offline ByerN
« Reply #16 - Posted 2018-05-13 19:34:50 »

Np Smiley
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (553 views)
2018-06-10 19:43:48

EgonOlsen (657 views)
2018-06-10 19:43:44

EgonOlsen (452 views)
2018-06-10 19:43:20

DesertCoockie (790 views)
2018-05-13 18:23:11

nelsongames (1025 views)
2018-04-24 18:15:36

nelsongames (1058 views)
2018-04-24 18:14:32

ivj94 (1637 views)
2018-03-24 14:47:39

ivj94 (562 views)
2018-03-24 14:46:31

ivj94 (1445 views)
2018-03-24 14:43:53

Solater (567 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!