android - NullPointerException on GSON Parsing -
i use gson parse json data, , app able parse google geocoder this. but, whenever try parse json website gives nullpointerexception
this eventresponse class makes connection btw gson , website
public class eventresponse { public meta meta; public objects[] objects; public eventresponse() {} public class meta { public int limit; public string next; public int offset; public string previous; public int total_count; } public class objects { public string category; public string date; public string description; public int id; public string name; public string resource_uri; public string venue; } }
this json
{ "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 7 }, "objects": [ { "category": "/api/v1/category/1/", "date": "2013-05-01", "description": "beş yakın arkadaş, ", //description longer in real "id": 10, "name": "kötü ruh / evil dead", "resource_uri": "/api/v1/event/10/", "venue": "/api/v1/venue/3/" }, { "category": "/api/v1/category/1/", "date": "2013-05-02", "description": "filmde, küçümseyen, ", "id": 9, "name": "korkunç bir film 5 / scary movie 5", "resource_uri": "/api/v1/event/9/", "venue": "/api/v1/venue/6/" } ] }
this how use gson (i tried objects[0] also, didn't matter)
gson gson = new gson(); eventresponse eresult = new eventresponse(); try { eresult = gson.fromjson(jsoncoord((caturl + categories.catid)), eventresponse.class); log.i("ama"+eresult.objects[1].category, "deneme");
jsoncoord method
public string jsoncoord(string address) throws ioexception { url url = new url(address); urlconnection connection = url.openconnection(); bufferedreader in = new bufferedreader(new inputstreamreader( connection.getinputstream())); string inputline; string jsonresult = ""; while ((inputline = in.readline()) != null) { jsonresult += inputline; } in.close(); return jsonresult; }
and cannot value log. error stack this: (line 59 log line)
05-02 21:48:32.020: w/system.err(17456): java.lang.nullpointerexception 05-02 21:48:32.030: w/system.err(17456): @ com.myapp.myapp.events.getvenue(events.java:59) 05-02 21:48:32.030: w/system.err(17456): @ com.myapp.myapp.events.oncreate(events.java:48) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activity.performcreate(activity.java:5206) 05-02 21:48:32.030: w/system.err(17456): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activitythread.performlaunchactivity(activitythread.java:2064) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activitythread.access$600(activitythread.java:140) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 05-02 21:48:32.030: w/system.err(17456): @ android.os.handler.dispatchmessage(handler.java:99) 05-02 21:48:32.030: w/system.err(17456): @ android.os.looper.loop(looper.java:137) 05-02 21:48:32.030: w/system.err(17456): @ android.app.activitythread.main(activitythread.java:4898) 05-02 21:48:32.030: w/system.err(17456): @ java.lang.reflect.method.invokenative(native method) 05-02 21:48:32.030: w/system.err(17456): @ java.lang.reflect.method.invoke(method.java:511) 05-02 21:48:32.030: w/system.err(17456): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 05-02 21:48:32.035: w/system.err(17456): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 05-02 21:48:32.035: w/system.err(17456): @ dalvik.system.nativestart.main(native method)
as has been pointed out, problem name of field.
in class name "objects", in json response, "objects" (lowercase). name of variable in program , in json must same, because gson allows json response fields you're interested in... if founds json value in response doesn't correspond of class attributes, gson ignore value.
you use annotation @serializedname
, in way:
@serializedname("objects") private objects[] whatevername;
this way can have different names in class , in json response.
note: type of attribute doesn't have problem. class objects
should written in uppercase follow java naming conventions...
Comments
Post a Comment