请求空点异常帮助

时间:2015-11-10 12:20:41

标签: java null

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.*;

public class Starter{

    public static void main(String[] args) throws Exception {
        BufferedReader fh =
                new BufferedReader(new FileReader("iot.txt"));
        //First line contains the language names
        String s = fh.readLine(); 
        List<String> langs =
                new ArrayList<>(Arrays.asList(s.split("\t")));
        langs.remove(0);    //Throw away the first word - "week"
        Map<String,HashMap<String,Integer>> iot = new TreeMap<>();
        while ((s=fh.readLine())!=null)
        {
            String [] wrds = s.split("\t");
            HashMap<String,Integer> interest = new HashMap<>();
            for(int i=0;i<langs.size();i++)
                interest.put(langs.get(i), Integer.parseInt(wrds[i+1]));
            iot.put(wrds[0], interest);
        }
        fh.close();
        HashMap<Integer,HashMap<String,HashMap<String,Integer>>>
            regionsByYear = new HashMap<>();
        for (int i=2004;i<2016;i++)
        {
            BufferedReader fh1 =
                    new BufferedReader(new FileReader(i+".txt"));
            String s1 = fh1.readLine(); //Throw away the first line
            HashMap<String,HashMap<String,Integer>> year = new HashMap<>();
            while ((s1=fh1.readLine())!=null)
            {
                String [] wrds = s1.split("\t");
                HashMap<String,Integer>langMap = new HashMap<>();
                for(int j=1;j<wrds.length;j++){
                    langMap.put(langs.get(j-1), Integer.parseInt(wrds[j]));
                }
                year.put(wrds[0],langMap);
            }
            regionsByYear.put(i,year);
            fh1.close();
        }

        int count = 0;

        for(String week:iot.keySet())
        {
            if (iot.get(week).get("C++")>iot.get(week).get("C#"))
            {
                count = count+1;
            }

        System.out.println("The number of weeks that there was more interest in C++ compared to C# is: "+count);
    }
  }
}

这是我的代码,目前我在行上发现java.lang.NullPointerException错误&#34; if (iot.get(week).get("C++")>iot.get(week).get("C#"))&#34;

我知道这是指周变量,但我不明白为什么。我之前使用过相同的代码块(没有计数)并且工作正常,所以为什么在添加count之后它会停止工作?

有没有人有解决方法?

0 个答案:

没有答案