Reputation: 1291
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.
Upvotes: 2
Views: 937
Reputation: 14159
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>()
);
Upvotes: 0
Reputation: 33534
- 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.
Upvotes: 0
Reputation: 37813
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>();
}
Upvotes: 5