Q:

Java roman to integer : null pointer

Anyone knows what is wrong with my pointer (value)?
Exception in thread "main" java.lang.NullPointerException
My program:
import java.util.LinkedHashMap;

public class Solution {
    private static LinkedHashMap<String, Integer> romanToNum = new LinkedHashMap<String, Integer>(); 
    static{
        romanToNum.put("M", 1000);
        romanToNum.put("D", 500);
        romanToNum.put("C", 100);
        romanToNum.put("L", 50);
        romanToNum.put("X", 10);
        romanToNum.put("V", 5);
        romanToNum.put("I", 1);
    }
    public int romanToInt(String s) {
        int sum = 0;
        char prev = '#';
        for(int i = s.length()-1; i >= 0; i--){
            char ch = s.charAt(i);
            ***int value = romanToNum.get(ch);***
            if(value < sum && ch != prev){
                sum -= value;
            }else{
                sum += value;
            }
            prev = ch;
        }
        return sum;
    }
}

A:

Your map is of type <String, Integer>, but you are using char primitives as the keys (for lookup):
char ch = s.charAt(i);
***int value = romanToNum.get(ch);***

These get autoboxed to Character objects, which are not compatible with Strings.  Thus, as others have noted, your map returns a null Integer, which gets auto-unboxed to a primitive int, and it is this operation which causes NPE to be thrown.
Solution 1:  Make the map of type <Character, Integer> instead.
Solution 2:  Change the offending line to:
***int value = romanToNum.get(String.valueOf(ch));***

A:

The problem is that you are calling get using a char, which is auto boxed to a Character.
char ch = s.charAt(i);
int value = romanToNum.get(ch);

Since no data is actually stored with Character keys, a null is returned. As others have pointed out, the null Integer is auto unboxed to an int, resulting in a NullPointerException.

