title: Why I get java.lang.NullPointerException when I use toCharArray() method?
body: <p>The question is Roman to Integer : <a href="https://leetcode.com/problems/roman-to-integer/" rel="nofollow">link</a>.
  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.</p>
  
  <pre><code>public int romanToInt(String s) {
        HashMap&lt;String, Integer&gt; dict = new HashMap&lt;String, Integer&gt;();
          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 &lt; arrayS.length ;  i++){
              int Val1 = dict.get(arrayS[i]).intValue();
              if(i &lt; arrayS.length -1){
              int Val2 = dict.get(arrayS[i+1]).intValue();
                  if(Val1 &lt; Val2){
                      sum += Val2 - Val1;
                      i++;}
                  else
                      sum += Val1;
              }
              else
                  sum += Val1;
           }
           return sum;
  
       }
  </code></pre>
  
  <p>Then in the position <code>int Val1 = dict.get(arrayS[i]);</code>, the complier shows <code>Line 19: java.lang.NullPointerException</code>. However, if I rewrite it without toCharArray(), it works.</p>
  
  <pre><code>int sum = 0;
          int val1, val2;
          for (int i = 0; i &lt; s.length(); i++) {
              val1 = map.get(s.charAt(i));
              if (i &lt; s.length()-1) {
                  val2 = map.get(s.charAt(i+1));
                  if (val1 &gt;= val2) {
                      sum += val1;
                  } else {
                      sum += (val2 - val1);
                      i += 1;
                  }
              } else {
                  sum += val1;
              }           
          }
          return sum;
  </code></pre>
  
  <p>Why do I meet <code>java.lang.NullPointerException</code> problem when using toCharArray()?</p>