-2

The question is Roman to Integer : link. Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. So I used toCharArray() method in my answer first.

public int romanToInt(String s) {
      HashMap<String, Integer> dict = new HashMap<String, Integer>();
        dict.put("I", 1);
        dict.put("V", 5);
        dict.put("X", 10);
        dict.put("L", 50);
        dict.put("C", 100);
        dict.put("D", 500);
        dict.put("M", 100);

        int sum = 0;
        char[] arrayS = s.toCharArray();
        for(int i = 0 ; i < arrayS.length ;  i++){
            int Val1 = dict.get(arrayS[i]).intValue();
            if(i < arrayS.length -1){
            int Val2 = dict.get(arrayS[i+1]).intValue();
                if(Val1 < Val2){
                    sum += Val2 - Val1;
                    i++;}
                else
                    sum += Val1;
            }
            else
                sum += Val1;
         }
         return sum;

     }

Then in the position int Val1 = dict.get(arrayS[i]);, the complier shows Line 19: java.lang.NullPointerException. However, if I rewrite it without toCharArray(), it works.

int sum = 0;
        int val1, val2;
        for (int i = 0; i < s.length(); i++) {
            val1 = map.get(s.charAt(i));
            if (i < s.length()-1) {
                val2 = map.get(s.charAt(i+1));
                if (val1 >= val2) {
                    sum += val1;
                } else {
                    sum += (val2 - val1);
                    i += 1;
                }
            } else {
                sum += val1;
            }           
        }
        return sum;

Why do I meet java.lang.NullPointerException problem when using toCharArray()?

JiPanNYC
  • 60
  • 7
  • 1
    When you debug this, which object is `null`? Where do you expect that object to be assigned a value? – David Mar 03 '16 at 14:43
  • 5
    Probably because `s` itself is `null`. – user2004685 Mar 03 '16 at 14:45
  • 3
    Just to get this clear: you really think, that when you ask a map with `String` as the key type to return anything else than `null`, when you ask it if it contains a certain `char`? Why do you think so? – Tom Mar 03 '16 at 14:48
  • @David` int Val1 = dict.get(arrayS[i]); `, I am not sure if arrayS[i] or dict.get(arrayS[i]) or Val1 is null pointer – JiPanNYC Mar 03 '16 at 15:18
  • @user2004685 No , I tried to test it with "DCXXI" and it still report me a Null Pointer Exception. – JiPanNYC Mar 03 '16 at 15:19
  • @JimmyBomb: You could use a debugger and *find out*. – David Mar 03 '16 at 15:20
  • @Tom Yes, you are totally right, Thank you Tom. You solved my problem. – JiPanNYC Mar 03 '16 at 15:23
  • @KevynMeganck Thank you for your advice, I read that article and Tom have pointed out my problem. – JiPanNYC Mar 03 '16 at 15:28

3 Answers3

0

dict.get(arrayS[i]); return null if there is no translation.
the unboxing of null from Integer to int causes the NullPointerException.

Yoav Gur
  • 1,366
  • 9
  • 15
  • Thank you for your suggestion. I forgot to do the transform. Then I edit it like `int Val1 = dict.get(arrayS[i]).intValue();` . But I still got the `NullPointerException`. :( – JiPanNYC Mar 03 '16 at 15:05
  • OP does exactly the same in alternative method – Alex Salauyou Mar 03 '16 at 15:06
  • Yes I guess it may not be the transformation problem. In the second method, i didn't do any transformations and it still worked. However, it's better to transform when unboxing. – JiPanNYC Mar 03 '16 at 15:09
  • @JimmyBomb Have you at least tried to think about my comment under the question? It points you to your main problem. – Tom Mar 03 '16 at 15:11
  • @Tom Thank you for reminding me. I am a beginner for using Stackoverflow Website. Sorry, I just noticed your comment. – JiPanNYC Mar 03 '16 at 15:26
0

The problem with your code is that you have created a HashMap that maps String to integer but you are passing a character as a parameter to the romanToInt method. You can solve the problem in two ways

Solution 1 -

public int romanToInt(String s) 
{
 HashMap<String, Integer> dict = new HashMap<String, Integer>();
 dict.put("I", 1);
 dict.put("V", 5);
 dict.put("X", 10);
 dict.put("L", 50);
 dict.put("C", 100);
 dict.put("D", 500);
 dict.put("M", 100);
 int sum = 0;
 char[] arrayS = s.toCharArray();
 for(int i = 0 ; i < arrayS.length ;  i++)
 {
  int Val1 = dict.get(String.valueOf(arrayS[i])); // Convert character  to string
  if(i < arrayS.length -1)
  {
   int Val2 = dict.get(String.valueOf(arrayS[i+1])); //Convert character to string
   if(Val1 < Val2)
   {
    sum += Val2 - Val1;
    i++;
   }
   else
    sum += Val1;
  }
  else
   sum += Val1;
 }
 return sum;
}

Solution 2 -

public int romanToInt(String s) 
{
 HashMap<Character, Integer> dict = new HashMap<Character, Integer>();
 //Map Character to Integer
 dict.put('I', 1); //Used single quote to represent character
 dict.put('V', 5);
 dict.put('X', 10);
 dict.put('L', 50);
 dict.put('C', 100);
 dict.put('D', 500);
 dict.put('M', 100);
 int sum = 0;
 char[] arrayS = s.toCharArray();
 for(int i = 0 ; i < arrayS.length ;  i++)
 {
  int Val1 = dict.get(arrayS[i]);
  if(i < arrayS.length -1)
  {
   int Val2 = dict.get(arrayS[i+1]);
   if(Val1 < Val2)
   {
    sum += Val2 - Val1;
    i++;
   }
   else
    sum += Val1;
  }
  else
   sum += Val1;
 }
 return sum;
}
Tejas
  • 302
  • 1
  • 4
  • 11
0

Actually I made a mistake in data type. I used Char array which the element is Char type. I tried to use dict.get(arrayS[i]) that the arrayS[i] should be a String type because I built this HashMap<String, Integer>. Then I changed the HashMap<String, Integer> to HashMap<Character, Integer> then it worked. Thank Tom @Tom for pointing out my problem.

JiPanNYC
  • 60
  • 7