27

The following code is giving me a NullPointerException. The problem is on the following line:

... 
dataMap.put(nextLine[0], nextLine[6]);

What is strange is that I have run this code without the above line and the call to nextLine[0] and nextLine[6] work exactly as expected - that is they give me back elements of a csv file. I declare and initialise the HashMap with the code

HashMap<String, String> dataMap = null;

earlier in the method

  String[] nextLine;
  int counter=0;
  while (counter<40) {
    counter++;

    System.out.println(counter);
    nextLine = reader.readNext(); 
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + " - " + nextLine[6] +" - " + "etc...");
    dataMap.put(nextLine[0], nextLine[6]);
  }
  return dataMap;
}
Eddie
  • 53,828
  • 22
  • 125
  • 145
Ankur
  • 50,282
  • 110
  • 242
  • 312
  • I just wonder about the "24" points for a such trivial newbie coding problem –  Feb 10 '21 at 13:03

6 Answers6

47
HashMap<String, String> dataMap = new HashMap<String,String>();

Your dataMap variable isn't initialized at this point. You should be getting a compiler warning about that.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Codingscape
  • 579
  • 4
  • 5
  • Isn't the problem really that it's *explicitly* initialised ? To null ? – Brian Agnew Apr 09 '09 at 16:13
  • I think Codingscape means to say "is not instantiated" – matt b Apr 09 '09 at 17:34
  • 1
    Actually, there is a hint here. He didn't get a warning because he initialized it to null to override the compiler warning he certainly was getting. You should make it a habit NOT to initialize variables to a "Neutral" value by default, only set them to their real value so the compiler can warn. – Bill K Apr 09 '09 at 17:39
5

Where is datamap initialised ? It's always null.

To clarify, you declare the variable and set it to null. But you need to instantiate a new Map, whether it's a HashMap or similar.

e.g.

datamap = new HashMap();

(leaving aside generics etc.)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Yes, it's very strange that dereferencing a pointer that is explicitly null would throw a NullPointerException. – Apocalisp Apr 09 '09 at 16:06
3

dataMap is declared but not initialized. It can be initialized with

datamap = new HashMap();

Sheldon
  • 391
  • 3
  • 7
1

Well, there are three objects accessed on that line. If nextLine[0] and nextLine[6] aren't null, because the println call above worked, then that leaves dataMap. Did you do dataMap = new HashMap(); somwehere?

jcopenha
  • 3,935
  • 1
  • 17
  • 15
0

My case was different as Usually, hashmaps throw the null pointer exception when trying to read a key that is not there e.g I have a

HashMap<String, Integer> map = new HashMap<String, Integer>();

Which is empty or have keys except "someKey", So when I try to

map.get("someKey") == 0;

Now this will produce a nullPointerExeption due to matching a null with some integer.

What should I do instead?

Ans: I should instead check for null like

map.get("someKey") != null;

Now the runtime error Nullpointer would not raise!

SaadurRehman
  • 622
  • 8
  • 20
-1

Um, what exactly do you expect when you do this?

HashMap<String, String> dataMap = null;
...
dataMap.put(...)
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 2
    I was expecting that null is kind of a place holder, but when you use put the value being "put" make the HashMap no longer equal to null – Ankur Apr 09 '09 at 16:09
  • If put =null everywhere will my program write itself? – mP. Apr 10 '09 at 04:51
  • ...and why a minus point here? I begin to doubt the objectivity of Stackoverflow community. –  Feb 10 '21 at 13:05