0
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);
    }
  }
}

This is my code, at the moment i get a java.lang.NullPointerException error on the line "if (iot.get(week).get("C++")>iot.get(week).get("C#"))"

I know that this is pointing to the week variable but i just don't understand why. I have used this same block of code (without the count) before and it worked fine so why after adding count in does it stop working?

Does anyone have any solution to fix this?

Avihoo Mamka
  • 4,656
  • 3
  • 31
  • 44
Mark
  • 47
  • 6
  • It is either that `iot.get(week)` is null, or that `iot.get(week).get("C++")` (or C#), not that `week` is null. – Andy Turner Nov 10 '15 at 12:21
  • The solution is either to use a debugger, or read your code and think carefully about what it is doing. – Stephen C Nov 10 '15 at 13:07
  • I realized I was really stupid. it was caused because in the text files that I'm getting my data from "c#" and "c++" are in lower case, I used upper case in my code. I'm actually an idiot. – Mark Nov 17 '15 at 10:29

0 Answers0