title: How to convert a String of Integers to an ArrayList in Java?
body: <p>I try to parse a textfile which has lines which look like the following:</p>
  
  <p><code>@KEY,_,0,1,2,_,4,5,6, ...</code></p>
  
  <p>The <code>@KEY</code> is just an identifier in the beginning while the following numbers are my data which I want to store in an <code>ArrayList&lt;Integer&gt;</code>.</p>
  
  <p>I have a metadata class which contains the arraylist in which I want to insert there integers:</p>
  
  <pre><code>class MetaD {
      public List&lt;Integer&gt; key1, key2, key3 = new ArrayList&lt;Integer&gt;();
  }
  </code></pre>
  
  <p>I parse the textfile line by line; when the line starts with <code>@KEY</code>, I want to add the elements to the <code>key1</code> list. If there is an <code>_</code>, it should be replaced with an empty value:</p>
  
  <pre><code>    if(line.startsWith("@KEY")){
          metaObject.key1 = Arrays.asList(line.replace("@KEY,", "").replace("_", "").trim().split("\\s*,\\s*"));
      }
  </code></pre>
  
  <p>I found out that this does not work with <code>ArrayList&lt;Integer&gt;</code>. <code>key1</code> has to be of the type <code>ArrayList&lt;String&gt;</code> or <code>ArrayList&lt;Object&gt;</code> to make it work.</p>
  
  <p>Is there a way to convert Integers in the same way?
  If not, my idea would be the following:</p>
  
  <ul>
  <li>Convert everything to an <code>ArrayList&lt;String&gt;</code></li>
  <li>Iterate every item of this new <code>ArrayList</code> and convert it with <code>Integer.parseInt()</code> into an Integer.</li>
  <li>Adding this new Integer to my <code>ArrayList&lt;Integer&gt;</code></li>
  </ul>
  
  <p>Would there be a more efficient or better way to archive my needs?</p>
  
  <hr>
  
  <p><strong>Edit:</strong>
  Since Tunaki wrote in the comments, that my idea will probably be the only possible way I tried to do the following:</p>
  
  <pre><code>    if(line.startsWith("@KEY")){
          List&lt;String&gt; channelTemp =  Arrays.asList(line.replace("@KEY,", "").replace("_", "1").split("\\s*,\\s*"));
          channelTemp.forEach(item -&gt; metaObject.channel.add(Integer.parseInt(item)));
          System.out.println("done");
      }
  </code></pre>
  
  <p>Unfortunately, this throws a <code>NullPointerException</code> in the third line here and I don't have a clue why. I replaced <code>_</code> with <code>1</code> for testing purposes to avoid a <code>NumberFormatException</code>. When I print out every object in the lambda function instead of adding them to my <code>ArrayList&lt;Integer&gt;</code>, I can see that all items have an Integer value. So why do I get an exception here?</p>