user3385759 user3385759 - 3 months ago 24
Java Question

Getting a NullPointerException when reading a file line by line.

I am doing the Introduction Course on Java at mooc.fi and I am stuck on exercice 35: Two-direction dictionary, where I have to build a dictionary. To be more specific, when I want to read a file (with my method load()) to add each line into my dictionary as an entry, I get a NullPointerException error.

These are the contents in my words.txt (my textfile which I want to read):
The left word is the key (Finnish word) and the right word is the value (English translation).

apina:monkey
alla oleva:below
olut:beer


This is my MindfulDictionary.java class

package dictionary;

import java.util.*;
import java.io.File;

public class MindfulDictionary {
private Map<String, String> entries;
private File file;
private Scanner reader;

public MindfulDictionary(){
this.entries = new HashMap<String, String>();
}

public MindfulDictionary(String file){
this.file = new File(file);
this.reader = null;
}

public boolean load(){
try{
this.reader = new Scanner(this.file);
}
catch (Exception e){
System.out.println("We couldn't load the file: " + e.getMessage());
return false;
}

while(this.reader.hasNextLine()){
String line = this.reader.nextLine();
String[] parts = line.split(":");
this.add(parts[0], parts[1]);
}
this.reader.close();
return true;
}

public void add(String word, String translation) {
if (!this.entries.containsKey(word) && !this.entries.containsValue(word)){
this.entries.put(word, translation);
}
}

public String translate(String word){
if (this.entries.containsKey(word)){
return this.entries.get(word);
}

for (Map.Entry<String, String> entry : this.entries.entrySet()){
if (entry.getValue().equals(word)){
return entry.getKey();
}
}
return null;
}

public void remove(String word){
if (this.entries.containsKey(word)){
this.entries.remove(word);
}

for (Map.Entry<String, String> entry : this.entries.entrySet()){
if (entry.getValue().equals(word)){
this.entries.remove(entry.getKey());
}
}
}
}


And my Main.java class

package dictionary;

public class Main {
public static void main(String[] args) {
MindfulDictionary dict = new MindfulDictionary("src/words.txt");
dict.load();


System.out.println( dict.translate("apina") );
System.out.println( dict.translate("ohjelmointi") );
System.out.println( dict.translate("alla oleva") );

}
}


And the error message:

Exception in thread "main" java.lang.NullPointerException
at dictionary.MindfulDictionary.add(MindfulDictionary.java:40)
at dictionary.MindfulDictionary.load(MindfulDictionary.java:33)
at dictionary.Main.main(Main.java:6)
Java Result: 1

Answer

You entries named Map is null.

You only initialize it in empty constructor but in the main method you are calling the constructor with String parameter:

public MindfulDictionary(){ 
  this.entries = new HashMap<String, String>();
}

 public MindfulDictionary(String file){  
   this.file = new File(file); 
   this.reader = null; 
 }

You must initialize it too in the second constructor

Comments