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.
nullor put every method call of the erroneous line on a separate line to see where exactly the exception happens.regionmapsomewhere?ifblock should be inside thewhileloop. BTW, what does the//DEBUGline prints?