I am trying to load a file into a Map with a method as below:
private static Map<String,Integer> indexVocabulary;
public static Map<String,Integer> getVocabularyFromFile() throws IOException
{
FileInputStream fstream = new FileInputStream(VOCABULARY_FILE);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
while( (line = br.readLine()) != null )
{
LOG.debug(line);
String[] kv = line.split(" ");
LOG.debug(kv[0]);
LOG.debug(Integer.toString(Integer.parseInt(kv[1])));
indexVocabulary.put(kv[0], Integer.parseInt(kv[1]));
}
return indexVocabulary;
}
I can see output from line‘also from kv[0],kv[1] and Integer.parseInt(kv[1]) However I get a NullPointerException on the line indexVocabulary.put(kv[0], Integer.parseInt(kv[1])); Does anyone know what is wrong with this method?
You don’t initialize
indexVocabulary, so it’snull.Change
to