title: Java 8: How to write lambda stream to work with JsonArray?
body: <p>I'm very new to Java 8 lambdas and stuff... I want to write a lambda function that takes a <code>JSONArray</code>, goes over its <code>JSONObject</code>s and creates a list of values of certain field.</p>
  <p>For example, a function that takes the <code>JSONArray</code>: <code>[{name: &quot;John&quot;}, {name: &quot;David&quot;}]</code> and returns a list of <code>[&quot;John&quot;, &quot;David&quot;]</code>.</p>
  <p>I wrote the following code:</p>
  <pre><code>import org.json.simple.JSONArray;
  import org.json.simple.JSONObject;
  
  public class Main {
      public static void main(String[] args) {
          JSONArray jsonArray = new JSONArray();
          jsonArray.add(new JSONObject().put(&quot;name&quot;, &quot;John&quot;));
          jsonArray.add(new JSONObject().put(&quot;name&quot;, &quot;David&quot;));
          List list = (List) jsonArray.stream().map(json -&gt; json.toString()).collect(Collectors.toList());
          System.out.println(list);
      }
  }
  </code></pre>
  <p>However, I get an error:</p>
  <blockquote>
  <p>Exception in thread &quot;main&quot; java.lang.NullPointerException</p>
  </blockquote>
  <p>DO you know how to resolve it?</p>