我想写从LinkedHashmap
到一个文件信息写入从LinkedHashMap的信息的时候,但是这个代码是给我一个NullPointerException
:NullPointerException异常到文件
public void writeFile(String file, Map<Word, Defination> wmap) {
File FileName = new File(file);
try {
FileOutputStream stream = new FileOutputStream(FileName, true);
PrintStream pstrim = new PrintStream(stream);
String line = null;
rmap=wmap;
Iterator<Word> it = wmap.keySet().iterator(); //generic iterator
while (it.hasNext() && found == 0) {
String key = it.next().getWord();
System.out.println(key);
Defination defns = new Defination(wmap.get(key).getDef()); //null pointer exception here
line = key + "=" + defns.getDef();
if (line != null) {
pstrim.println(line);
}
}
stream.close();
} catch (IOException IO) {
}
}
为什么有NullPointerException
和哪能修理它?
你的wmap.get(键)似乎返回null ..你有对应的键值吗? – TheLostMind
'.getDef()'也可能是空的。 –
@RyanCarlson - 这不会给NullPointerException ...如果getDef()返回null,我们不会在其他任何地方使用它... – TheLostMind