2

I am currently writing a command line program that is supposed to take in lines from a text file and add data to an array of HashMaps. I currently get a NullPointerException when running this method.

Public class Vaerdata {
String[] linje;
String line;
String line2;
HashMap<String, Stasjon> stasjonsmap = new HashMap<String, Stasjon>();
HashMap<String, Stasjon>[] regionmap = (HashMap<String, Stasjon>[]) new HashMap<?, ?>[6];


void init() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("stasjoner_norge.txt"));
    BufferedReader brData = new BufferedReader(new FileReader("klimadata2012.txt"));
    for(int i = 0; i < 10; i++){
        brData.readLine();
    }
    br.readLine();
    while((line = br.readLine()) != null){
        linje = line.split("\\s+");
        stasjonsmap.put(linje[1], new Stasjon(Integer.parseInt(linje[1]), linje[2], Integer.parseInt(linje[3]), linje[4], linje[5], linje[6]));
        }
        if(linje[6].equals("AGDER")){
            System.out.println(stasjonsmap.get(linje[1])); //DEBUG
            regionmap[1].put(stasjonsmap.get(linje[1]).navn, stasjonsmap.get(linje[1]));
            System.out.println(regionmap[1].get(stasjonsmap.get(linje[1]).navn)); //DEBUG
        }
    }  
}

My NullPointerException happens in this line:

regionmap[1].put(stasjonsmap.get(linje[1]).navn, stasjonsmap.get(linje[1]));

So my question is: When I have declared the array of HashMaps with <String, Stasjon> (Stasjon is an object of my Stasjon class, taking info about certain weather stations), why do I get an error in that line? The object in stasjonsmap.get(linje[1]) has been declared, and I can't understand why it won't allow me to make a reference to this object in my second hashmap.

Every line in the text file, after line one (which I skip in my program) look like this:
36200 TORUNGEN_FYR 12 ARENDAL AUST-AGDER AGDER

In advance; thanks for your help.

3
  • 4
    General advice: Use a debugger to see which reference is null or put every method call of the erroneous line on a separate line to see where exactly the exception happens. Commented Nov 1, 2012 at 9:08
  • Do you populate regionmap somewhere? Commented Nov 1, 2012 at 9:08
  • I think, the if block should be inside the while loop. BTW, what does the //DEBUG line prints? Commented Nov 1, 2012 at 9:09

3 Answers 3

5

When you initialize your array of HashMap here

HashMap<String, Stasjon>[] regionmap = (HashMap<String, Stasjon>[]) new HashMap<?, ?>[6];

all values in the array are null.

You then try to call the put method of HashMap on a null-reference.

First you have to initialize your HashMaps somehow:

for (int i = 0; i < regionmap.length; i++) {
    regionmap[i] = new HashMap<String, Stasjon>();
}
Sign up to request clarification or add additional context in comments.

1 Comment

The problem has been resolved, thank you very much. I included a for-loop to initialize all the HashMaps in the regionmap array at the beginning of my init() method. Thanks!
0

- I think you have not initialized the HashMap, and you called put() method.

- As HashMap is an Object its default value is null, so you need to initialize it.

Comments

0

You have created an array of Hashmaps of size 6, but all items in that array are still initialized to null.

You could instead do it like this (which also gets rid of the ugly cast):

  private static HashMap<String, Stasjun>[] initRegionMap(HashMap<String, Stasjun>... items) {
    return items;
  }

  HashMap<String, Stasjun>[] regionmap = initRegionMap ( 
      new HashMap<String, Stasjun>(),
      new HashMap<String, Stasjun>(),
      new HashMap<String, Stasjun>(),
      new HashMap<String, Stasjun>(),
      new HashMap<String, Stasjun>(),
      new HashMap<String, Stasjun>()
    );

2 Comments

What if he needs 1000 instead of 6 next time? :D
Code evolves... He'd have to use a loop. But depending on the actual problem he's trying to solve, perhaps another data structure would fit even better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.