0

I have this code:

booleans.put(varToSet, choiceResponse);

and it throws a NullPointerException. The error it shows is this:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.salsagames.prometheus.code.Compiler$2.either(Compiler.java:120)
    at com.salsagames.prometheus.code.message.YesNo.lambda$new$2(YesNo.java:73)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

The two variables I use are:

varToSet test

choiceResponse true

I am using the right types for the Map, (String in, Boolean out).

My Map needed to be declared as new HashMap<>()

Salsa Gal
  • 45
  • 1
  • 8
  • 1
    Have you tested what variable is null on that line? For instance, have you checked if the `booleans` Map variable is null? I suspect that it is -- but check it to be sure. Then look back in your code to see why. – Hovercraft Full Of Eels Apr 02 '18 at 12:52
  • @Hovercraft Full Of Eels The supposed duplicate doesn't help because the variables have been declared, and the Map doesn't need to be declared because it is abstract. – Salsa Gal Apr 02 '18 at 12:54
  • 1
    Your statement above doesn't make sense, since NullPointerExceptions (NPE) have nothing to do with variables being *declared* and all to do with their being *initialized*, and you didn't answer my question (whether it helps or not). Have you tested if it is null or not? Place a line above the offending line, something like `System.out.println("is booleans null?: " + (booleans == null));` – Hovercraft Full Of Eels Apr 02 '18 at 12:55
  • Sorry, worded things badly. – Salsa Gal Apr 02 '18 at 12:57
  • And don't worry, I've figured out what was wrong. Simple mistake. – Salsa Gal Apr 02 '18 at 12:57
  • Can you please unmark this as a duplicate because the other question is about NullPointerExceptions in general, while this is about Map not being declared in a way that is unique to Maps. I didn't know you needed to use HashMap. – Salsa Gal Apr 02 '18 at 12:59
  • How do I answer my question? – Salsa Gal Apr 02 '18 at 13:00
  • I will consider removing the duplicate if you prove that it is not a duplicate and as written this isn't so. Please consider re-writing your question, posting all error messages and creating and posting a valid [mcve] program to go with it. – Hovercraft Full Of Eels Apr 02 '18 at 13:00
  • As currently written it is still yet another NPE question, and we get hundreds of these daily. No need to answer it as currently written. – Hovercraft Full Of Eels Apr 02 '18 at 13:01
  • But don't you think that this more specific one would be helpful? – Salsa Gal Apr 02 '18 at 13:03
  • Again, update/improve the question -- let's see – Hovercraft Full Of Eels Apr 02 '18 at 13:04
  • Well is it good now? – Salsa Gal Apr 02 '18 at 13:05
  • Ah I've seen your update, no, no reason to re-open the question as it is the same as all the others. It has nothing to do with Map or HashMap per say. The error is solely due to your using a variable before assigning an object to it. Same as all the hundreds of thousands of similar questions. Delete it as it will help no one. Seriously. – Hovercraft Full Of Eels Apr 02 '18 at 13:05
  • Yeah, but I didn't know I needed HashMap. Oh well. People probably aren't as stupid as me and knew that. – Salsa Gal Apr 02 '18 at 13:06
  • You're doing `Foo foo;` then `foo.SomeMethodCall();` before doing `Foo foo = new Foobar();` same as all the others. – Hovercraft Full Of Eels Apr 02 '18 at 13:06
  • OK, fair enough. – Salsa Gal Apr 02 '18 at 13:07
  • [Similar questions](https://www.google.com/search?&q=site%3Ahttps%3A%2F%2Fstackoverflow.com%2F+java+NullPointerException+Map) – Hovercraft Full Of Eels Apr 02 '18 at 13:07
  • OK then. Leave it as it is. – Salsa Gal Apr 02 '18 at 13:08
  • [Check this question in particular](https://stackoverflow.com/questions/26559768/null-check-in-map-gets-null-pointer-exception). Again this sort of thing has been asked a gabillion times before – Hovercraft Full Of Eels Apr 02 '18 at 13:11

0 Answers0