title: NullPointerException in JSONArray
body: <p>I am reading from a JSON ReST API. And the JSON looks different
  The first looks like this :</p>
  <pre><code>processJSON({
    &quot;TripList&quot;:{
    &quot;noNamespaceSchemaLocation&quot;:&quot;http://api.vasttrafik.se/v1/hafasRestTrip.xsd&quot;,
    &quot;servertime&quot;:&quot;10:39&quot;,
    &quot;serverdate&quot;:&quot;2013-04-04&quot;,
    &quot;Trip&quot;:[{
      &quot;Leg&quot;:[{
        &quot;name&quot;:&quot;Buss 761&quot;,
        &quot;type&quot;:&quot;BUS&quot;,
        &quot;id&quot;:&quot;9015014576100035&quot;,
        &quot;direction&quot;:&quot;Heden via Linnéplatsen&quot;,
        &quot;fgColor&quot;:&quot;#00A5DC&quot;,
        &quot;bgColor&quot;:&quot;#ffffff&quot;,
        &quot;stroke&quot;:&quot;Solid&quot;,
        &quot;Origin&quot;:{
          &quot;name&quot;:&quot;Hökegårdsgatan, Mölndal&quot;,
          &quot;type&quot;:&quot;ST&quot;,
          &quot;id&quot;:&quot;9022014012273002&quot;,
          &quot;routeIdx&quot;:&quot;30&quot;,
          &quot;time&quot;:&quot;10:42&quot;,
          &quot;date&quot;:&quot;2013-04-04&quot;,
          &quot;track&quot;:&quot;B &quot;,
          &quot;$&quot;:&quot;\n&quot;
          }
  </code></pre>
  <p>the second like this :</p>
  <pre><code>processJSON({
  &quot;TripList&quot;:{
    &quot;noNamespaceSchemaLocation&quot;:&quot;http://api.vasttrafik.se/v1/hafasRestTrip.xsd&quot;,
    &quot;servertime&quot;:&quot;10:46&quot;,
    &quot;serverdate&quot;:&quot;2013-04-04&quot;,
    &quot;Trip&quot;:[{
      &quot;Leg&quot;:{
        &quot;name&quot;:&quot;Spårvagn 8&quot;,
        &quot;type&quot;:&quot;TRAM&quot;,
        &quot;id&quot;:&quot;9015014500800075&quot;,
        &quot;direction&quot;:&quot;Angered&quot;,
        &quot;fgColor&quot;:&quot;#872387&quot;,
        &quot;bgColor&quot;:&quot;#ffffff&quot;,
        &quot;stroke&quot;:&quot;Solid&quot;,
        &quot;accessibility&quot;:&quot;wheelChair&quot;,
        &quot;Origin&quot;:{
          &quot;name&quot;:&quot;Korsvägen, Göteborg&quot;,
          &quot;type&quot;:&quot;ST&quot;,
          &quot;id&quot;:&quot;9022014003980005&quot;,
          &quot;routeIdx&quot;:&quot;12&quot;,
          &quot;time&quot;:&quot;10:50&quot;,
          &quot;date&quot;:&quot;2013-04-04&quot;,
          &quot;track&quot;:&quot;E &quot;,
          &quot;rtTime&quot;:&quot;10:51&quot;,
          &quot;rtDate&quot;:&quot;2013-04-04&quot;,
          &quot;$&quot;:&quot;\n&quot;
          }
  </code></pre>
  <p>Now what I wanna do is if the JSON &quot;Leg&quot; is an Array I loop into the Array and get the data.
  But if the &quot;Leg&quot; is an object there is no use to loop I can just dig and get the data.
  This one is working for me</p>
  <p>But the first is not working and gives an nullpointerexpection.</p>
  <h3>My code is :</h3>
  <pre><code>public class ParseTimeTable {
  
  String temp = &quot;&quot;;
  
  public void parseJsonData() throws ParseException, IOException {
  
      JSONParser parser = new JSONParser();
      Object obj = parser.parse(Planner.getPlanner().getJsonDataForTrip(
              Planner.getPlanner().getStartLocationID(),
              Planner.getPlanner().getDestinationID()));
      JSONObject topObject = (JSONObject) obj;
      JSONObject tripList = (JSONObject) topObject.get(&quot;TripList&quot;);
      JSONArray array = (JSONArray) tripList.get(&quot;Trip&quot;);
      Iterator&lt;JSONObject&gt; iterator = array.iterator();
  
      while (iterator.hasNext()) {
  
          JSONObject jsonObj = iterator.next();
  
          if (jsonObj.get(&quot;Leg&quot;) instanceof JSONArray)
  
          {
  
              JSONArray array2 = (JSONArray) tripList.get(&quot;Leg&quot;);
              Iterator&lt;JSONObject&gt; iterator2 = array2.iterator();
              while (iterator2.hasNext()) {
  
                  String line = (String) jsonObj.get(&quot;name&quot;);
                  String direction = (String) jsonObj.get(&quot;direction&quot;);
                  JSONObject origin = (JSONObject) jsonObj.get(&quot;Origin&quot;);
                  String name = (String) origin.get(&quot;name&quot;);
                  String time = (String) origin.get(&quot;time&quot;);
                  String track = (String) origin.get(&quot;track&quot;);
  
                  JSONObject destination = (JSONObject) jsonObj.get(&quot;Destination&quot;);
                  String dname = (String) destination.get(&quot;name&quot;);
                  String dtime = (String) destination.get(&quot;time&quot;);
                  String dtrack = (String) destination.get(&quot;track&quot;);
  
                  temp = line + &quot; &quot; + &quot;Mot : &quot; + direction + &quot; &quot; + &quot;Från : &quot;
                          + name + &quot; &quot; + &quot;Avgång : &quot; + time + &quot; &quot;
                          + &quot;Track : &quot; + track + &quot;- &quot; + &quot;Destination : &quot;
                          + dname + &quot; &quot; + &quot;Ankomst : &quot; + dtime + &quot; &quot;
                          + &quot;Track : &quot; + dtrack;
  
                  TripService.getTrip().setList(temp);
  
              }
  
          }
  
          else {
  
              jsonObj = (JSONObject) jsonObj.get(&quot;Leg&quot;);
              String line = (String) jsonObj.get(&quot;name&quot;);
              String direction = (String) jsonObj.get(&quot;direction&quot;);
              JSONObject origin = (JSONObject) jsonObj.get(&quot;Origin&quot;);
              String name = (String) origin.get(&quot;name&quot;);
              String time = (String) origin.get(&quot;time&quot;);
              String track = (String) origin.get(&quot;track&quot;);
  
              JSONObject destination = (JSONObject)                           jsonObj.get(&quot;Destination&quot;);
              String dname = (String) destination.get(&quot;name&quot;);
              String dtime = (String) destination.get(&quot;time&quot;);
              String dtrack = (String) destination.get(&quot;track&quot;);
  
              temp = line + &quot; &quot; + &quot;Mot : &quot; + direction + &quot; &quot; + &quot;Från : &quot;
                      + name + &quot; &quot; + &quot;Avgång : &quot; + time + &quot; &quot; + &quot;Track : &quot;
                      + track + &quot;- &quot; + &quot;Destination : &quot; + dname + &quot; &quot;
                      + &quot;Ankomst : &quot; + dtime + &quot; &quot; + &quot;Track : &quot; + dtrack;
  
              TripService.getTrip().setList(temp);
  
          }
  
      }
  
  }
  </code></pre>
  <p>}</p>
  <p>So everything in my else statement is working like it should but in my if statement I get a nullpointer because the array2 is empty..
  So what is giving me a nullpointer is my second Array : JSONArray array2 = (JSONArray) tripList.get(&quot;Leg&quot;);</p>
  <h3>Why is it so?</h3>
  <pre><code>Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.NullPointerException
      at se.lowdin.trafficPlanner.ParseTimeTable.parseJsonData(ParseTimeTable.java:35)
      at se.lowdin.trafficPlanner.MainWindow$UpdateAction.actionPerformed(MainWindow.java:294)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Window.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>