1

I have tried to figure this out on my own... starting with two excellent tutorials on GSON parsing from High-Speed Blog and Java Code Geeks. 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:

            {
                "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"
                    }
                ]
            }

my root class (jsonPlots) and 'nested' class (Plants) are as follows:

    public class jsonPlots {
      @SerializedName("Field")
        public String mFieldId;
      public ArrayList<Plants> 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;

    }

I have verified that I am getting a valid response using the following snippet:

    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<Plants> 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

The first Toast (mFieldID) works. It crashes at if(results.isEmpty()) 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.

SolarBlitz
  • 63
  • 1
  • 7

1 Answers1

1

Gson doesn't know that you want to bind the jsonPlots.PlantArray attribute to the "Plants" JSON element, because the names don't match and there isn't any configuration information (e.g., @SerializedName("Plants")) to otherwise let Gson know to bind them.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
  • bruce Thanks for the observation. I'm sorry, but I don't know how implement what you are talking about... which is why I'm asking the question. Would you be kind enough to provide a suggestion / correction to the code ? – SolarBlitz Jun 10 '11 at 16:14
  • bruce was correct. I changed the 'root' class (jsonPlots) as follows: public class jsonPlots { @SerializedName("Field") public String mFieldId; @SerializedName("Plants") public ArrayList PlantArray; } – SolarBlitz Jun 10 '11 at 16:19