title: Android update SQLite with GSON response ... how should I parse nested JSON?
body: <p>I have tried to figure this out on my own...  starting with two excellent tutorials on GSON parsing  from <a href="http://blog.brianbuikema.com/2010/04/android-how-to-deserialize-both-xml-and-json/" rel="nofollow">High-Speed Blog</a> and <a href="http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html" rel="nofollow">Java Code Geeks</a>.  Several [answers] (http://stackoverflow.com/questions/3763937/gson-and-deserializing-an-array-of-objects-with-arrays-in-it/6300251#6300251) on stackoverflow are relevant, but I need more help than a screendump.  I want to update my SQLite database from the following JSON response:</p>
  
  <pre><code>            {
                  "Field": "13",
                  "Plants": [
                      {
                          "PlantID": 123,
                          "PlotID": 321,
                          "Row": 1,
                          "Post": 1,
                          "Position": 1,
                          "PlotName": "Sunnyside",
                          "Breed": "Daisy"
                      },
                      {
                          "PlantID": 149,
                          "PlotID": 348,
                          "Row": 1,
                          "Post": 20,
                          "Position": 1,
                          "PlotName": "Waterside",
                          "Breed": "Iris"
                      }
                  ]
              }
  </code></pre>
  
  <p>my root class (jsonPlots) and 'nested' class (Plants) are as follows:</p>
  
  <pre><code>    public class jsonPlots {
        @SerializedName("Field")
          public String mFieldId;
        public ArrayList&lt;Plants&gt; PlantArray;
      }
      public class Plants {
          @SerializedName("PlantID")
          public int jPlantID;
  
          @SerializedName("PlotID")
          public int jPlotID;
  
          @SerializedName("Row")
          public int jRow; 
  
          @SerializedName("Post")
          public int jPost;
  
          @SerializedName("Position")
          public int jPosition;
  
          @SerializedName("PlotName")
          public String jPlotName;
  
          @SerializedName("Breed")
          public String jBreed;
  
      }
  </code></pre>
  
  <p>I have verified that I am getting a valid response using the following snippet:</p>
  
  <pre><code>    public void getJsonData() {
        InputStream source = retrieveStream(url);     
        Gson gson = new Gson();      
        Reader reader = new InputStreamReader(source);      
        jsonPlots response = gson.fromJson(reader, jsonPlots.class);
        Toast.makeText(this, response.mFieldId, Toast.LENGTH_SHORT).show();  // this works
      ArrayList&lt;Plants&gt; results = response.PlantArray;
  
      if(results.isEmpty()) { ///    null pointer exception here
        Toast.makeText(this, "no data to update", Toast.LENGTH_SHORT).show();
          }
      else {
          for (Plants result : results) {
                try {
                    Toast.makeText(this, result.jBreed, Toast.LENGTH_SHORT).show();  //  replace with SQLite update
                    Toast.makeText(this, result.jPlotName, Toast.LENGTH_SHORT).show();
                  }
                finally {
                    Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
                  }
          }
      }
  }  // getJsonData
  </code></pre>
  
  <p>The first Toast (mFieldID) works.  It crashes at <strong>if(results.isEmpty())</strong> with a null pointer exception as noted.   Is my ArrayList not populating ?   I would like a code suggestion on how to sequence through this nested array... so that I can systematically update my SQLite database.</p>