in my code, I am trying to compare an Integer
with int
as the condition for an if
statement, however it returns a NullPointerException
error. I have come across answers for the equivalence of the two, but not greater than / less than.
Here is the code I have so far: (the relevant line is if (val > maxVal)
)
import java.util.*;
import java.io.*;
class Untitled {
public static void main(String[] args) throws FileNotFoundException
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter filename: ");
Scanner inFile = new Scanner(new File(sc.nextLine()));
int maxVal = 0;
String maxKey = null;
TreeMap<String,Integer> tmap = new TreeMap<String,Integer>();
while (inFile.hasNextLine())
{
String key = inFile.nextLine();
Integer val = tmap.get(key);
tmap.put(key, (val==null) ? 1 : val+1);
if (val > maxVal) {
maxVal = val;
maxKey = key;
}
}
inFile.close();
System.out.println(maxKey+" "+maxVal);
}
}
Thanks! (I am currently learning Java and this is my first time posting on Overflow, so sorry for any errors I made).