java中映射上的NullPointerException。怎么了?

java中映射上的NullPointerException。怎么了?,java,file,map,nullpointerexception,Java,File,Map,Nullpointerexception,我正在尝试使用以下方法将文件加载到地图中: private static Map<String,Integer> indexVocabulary; public static Map<String,Integer> getVocabularyFromFile() throws IOException { FileInputStream fstream = new FileInputStream(VOCABULARY_FILE); DataInputStre

我正在尝试使用以下方法将文件加载到地图中:

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;
}
私有静态映射索引词汇表;
公共静态映射getVocabularyFromFile()引发IOException
{
FileInputStream fstream=新的FileInputStream(词汇表\u文件);
DataInputStream in=新的DataInputStream(fstream);
BufferedReader br=新的BufferedReader(新的InputStreamReader(in));
弦线;
而((line=br.readLine())!=null)
{
LOG.debug(行);
串[]kv=线路分裂(“”);
LOG.debug(kv[0]);
LOG.debug(Integer.toString(Integer.parseInt(kv[1]));
index词汇表.put(kv[0],Integer.parseInt(kv[1]);
}
返回索引词汇;
}

我可以看到
line
'的输出,也可以看到
kv[0]、kv[1]
Integer.parseInt(kv[1])
但是我在
index词汇表.put(kv[0]、Integer.parseInt(kv[1])行上得到一个NullPointerException有人知道这个方法有什么问题吗

您忘了初始化
索引词汇表

只要做:

indexVocabulary = new HashMap<String, Integer>();
index词汇表=newhashmap();

您忘记初始化
索引词汇表了

只要做:

indexVocabulary = new HashMap<String, Integer>();
index词汇表=newhashmap();

您没有初始化
索引词汇表
,因此它是
空的

改变

private static Map<String,Integer> indexVocabulary;
私有静态映射索引词汇表;

private static Map index词汇表=new HashMap();

您没有初始化
索引词汇表
,因此它是
空的

改变

private static Map<String,Integer> indexVocabulary;
私有静态映射索引词汇表;

private static Map index词汇表=new HashMap();

您没有将任何对象分配给
索引词汇表
,因此它为空。就这样宣布吧:

private static Map<String,Integer> indexVocabulary = new HashMap<String,Integer>;
private static Map index词汇表=新HashMap;

您没有将任何对象分配给
索引词汇表
,因此它为空。就这样宣布吧:

private static Map<String,Integer> indexVocabulary = new HashMap<String,Integer>;
private static Map index词汇表=新HashMap;

您尚未实例化地图。简单地说


index词汇表=newhashmap()

您尚未实例化地图。简单地说


index词汇表=newhashmap()

您没有初始化映射,它只是一个空变量。您需要实例化地图,例如,您可以执行以下操作:

indexVocabulary = new HashMap<String,Integer>();
index词汇表=newhashmap();

您没有初始化映射,它只是一个空变量。您需要实例化地图,例如,您可以执行以下操作:

indexVocabulary = new HashMap<String,Integer>();
index词汇表=newhashmap();