Hos*_*ein 1 java file map nullpointerexception
我正在尝试使用以下方法将文件加载到Map中:
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;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到输出line"也kv[0],kv[1]和Integer.parseInt(kv[1])不过我上线一个NullPointerException indexVocabulary.put(kv[0], Integer.parseInt(kv[1]));有谁知道什么是错用这种方法?
你没有初始化indexVocabulary,所以它是null.
更改
private static Map<String,Integer> indexVocabulary;
Run Code Online (Sandbox Code Playgroud)
至
private static Map<String,Integer> indexVocabulary = new HashMap<String,Integer>();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
150 次 |
| 最近记录: |