0

Using Retrofit2 to parse JSON in but getting NULL pointer exception & I don't know why? JSON response:

{
"query": {
    "count": 1,
    "created": "2016-04-29T17:19:45Z",
    "lang": "US",
    "results": {
        "quote": [{
            "name": "Talon",
            "tix": "50.0000",
            "avg": "34757600",
                 }]
              }
        }
}

Query class:

public class Query {

@SerializedName("query")
private String query = null;

}

Results class:

public class Results {

@SerializedName("results")
private String results = null;

@SerializedName("results")
private List<Quote> Quotes;
public List<Quote> getQuotes() {
    return Quotes;
}
}

Quote class:

public class Quote {

@SerializedName("name")
private String Name;
public String getName() {
    return Name;
}

@SerializedName("tix")
private String Tix;
public String getTix() {
    return Tix;
}

@SerializedName("avg")
private String Avg;
public String getAvg() {
    return Avg;
}

}

Error returned in Android Studio logcat: java.lang.NullPointerException: storage == null at Activity.java:60. That line contains:

data = new ArrayList<>(Arrays.asList(jsonResponse.getQuotes()));

I'm not certain if the NULL is because the model classes aren't correct or something else & uncertain how/where to begin to fix it? I've looked at 4 other questions that have the same error but none were using Retrofit or JSON...

JC23
  • 1,248
  • 4
  • 18
  • 28
  • 1
    You have two `@SerializedName("results")`. I'm guessing Retrofit only looks at the first one. – OneCricketeer May 02 '16 at 16:39
  • You might also want to change your JSON schema to return an array for results instead of key-valued quote objects. – OneCricketeer May 02 '16 at 16:40
  • At first your query class is wrong. Query is an json object and not a String. Gson can't parsed it and thats why it is null. Use this plugin for Android Studio to generate the righjt Gson Objects https://github.com/zzz40500/GsonFormat – Ralph Bergmann May 02 '16 at 16:51
  • Thanks for feedback @RalphBergmann. Added the plugin you provided but the same error remains? Tried jsonschema2pojo too? Made mistake in the initial JSON post as I do return array for Quote. Can't seem to wrap my head around this one... – JC23 May 04 '16 at 23:04
  • @cricket_007 I made error in JSON post, return array for Quote. Just corrected it – JC23 May 04 '16 at 23:07

0 Answers0