0

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).

  • 1
    For a [mcve], you should include example input/output. (however in this particular example it happens to be unnecessary, as the problem exists for all (non-empty) input) – user202729 Aug 05 '18 at 05:17

2 Answers2

2

Simplest solution I see is to swap your ternary to the assignment of val (and you might as well use int). Also, I would prefer Map.getOrDefault(Object, V). Like,

int val = 1 + tmap.getOrDefault(key, 0);
tmap.put(key, val);
if (val > maxVal) {
    maxVal = val;
    maxKey = key;
}

Otherwise, you could repeat the ternary in your comparison (but I personally think that's ugly so I'll leave it as a completely optional exercise for the reader).

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thanks!! That fixed it! If you don't mind clarifying, how is `1 + tmap.getOrDefault(key, 0)` able to replace the ternary? I read the Docs about .getOrDefault` but couldn't really understand it. –  Aug 05 '18 at 05:24
  • @RichardRobinson The value you want is one plus the existing value (which is zero if the value doesn't exist in the map). Your ternary is the same (logically). You could also have written `Integer val = tmap.containsKey(key) ? 1 + tmap.get(key) : 1;` - there are many ways to *express* things. – Elliott Frisch Aug 05 '18 at 05:34
0

It is giving you a NullPointerException because the value you are comparing is null (the value you are getting from your map is null), not because of a comparison between int and Integer.

The first time through your loop the map is empty, so your call to get will return null. Then you are attempting to check whether val (which is null) is less than maxVal. You are immediately inserting a 1 if val is null, but your subsequent comparison is not guarding against val being null.


The Map get() method also returns null if the key is not found,

Roshana Pitigala
  • 8,437
  • 8
  • 49
  • 80
Bill
  • 1,407
  • 10
  • 18
  • "_It is **not** giving you a NullPointerException..._" what are you talking about? The OP states that it gives a `NullPointerException`, "_however it returns a NullPointerException..._" – Roshana Pitigala Aug 05 '18 at 05:28
  • You didn't read the whole sentence. I said "It is not giving you a NullPointerException because of a comparison between int and Integer, but because the value you are comparing is null." I never said it wasn't giving a NullPointerException. The OP seemed to imply that the NPE may be coming from comparing an int with and Integer, and that is not the case. – Bill Aug 05 '18 at 05:30
  • Sorry your answer was quite unclear to me. Removed the downvote. :) – Roshana Pitigala Aug 05 '18 at 05:39