编辑我的问题:
我一直致力于我的第一个编程和Android项目。
在以下代码中,我收到了:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
尝试在我的结果 JSONOBJECT上调用方法,该方法是使用网络库https://github.com/koush/ion检索的,并且正在使用Google GSON库进行解析。
toast字符串消息和log.v显示JSON对象正在成功传递,包含所有必需的元素。(或者可能没有完全传递?Log.v会在某个点之后切断。)
我的困惑在于能够在结果上调用toString()GSON方法,这会导致成功的Toast消息,同时尝试调用任何其他GSON方法,如result.get(images_count)。 toString导致我的错误。我尝试在执行代码之前使用if(result.isJsonNull())以确保结果不为null,但即使result.isJsonNull()返回false,我的方法调用也会返回错误。
我的2部分问题
是什么导致我返回的JsonObject 结果成功执行某些GSON库方法(比如toString()),同时为其他人返回NullPointer异常,就像我尝试通过json元素成员到达.get(字符串成员)?
将来,如何在对象可能返回NullPointerException的地方放置/识别检查点?到目前为止,我只使用Logging或Toasts来显示当前对象/变量值。感谢BRONX的回答。我必须使用断点。
导致NullPointer异常的代码:(我已经用xxxxxxx替换了我的CLient-ID,以保证它在互联网上的秘密)。
Future<JsonObject> loading;
private void load() {
// don't attempt to load more if a load is already in progress
if (loading != null && !loading.isDone() && !loading.isCancelled())
return;
// load the tweets from album : gQxwy https://api.imgur.com/3/album/{id}
//https://api.imgur.com/3/album/{id}/images
String url = "https://api.imgur.com/3/album/gQxwy.json";
// This request loads a URL as JsonObject and invokes
// a callback on completion.
loading = Ion.with(this)
.load(url)
.setHeader("Authorization", "Client-ID " + "xxxxxxxxxxxxxxx")
.setLogging("ION_VERBOSE_LOGGING", Log.VERBOSE)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
// this is called back onto the ui thread, no Activity.runOnUiThread or Handler.post necessary.
if (e != null) {
Toast.makeText(Imgur.this, "Error loading tweets", Toast.LENGTH_LONG).show();
return;
}
//Here i test if the object is Null.It goes directly to excute else, showing that the object is not Null.
if(result.isJsonNull()){
Toast.makeText(Imgur.this, result.toString(), Toast.LENGTH_LONG).show();
}else {
Log.v("JSONOBJECT_STRING", result.toString());
Toast.makeText(Imgur.this, result.get("images_count").toString(), Toast.LENGTH_LONG).show();
}
}
});
}
这是LogCat,它包含成功检索的JSON对象(我们可以在日志中看到JSON对象。)Json对象包含元素images_count。 logcat的:
03-21 21:24:26.395 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (353 ms) https://api.imgur.com/3/album/gQxwy.json: Connection successful
03-21 21:24:26.405 8035-8061/? D/ION_VERBOSE_LOGGING﹕ (363 ms) https://api.imgur.com/3/album/gQxwy.json: Recycling keep-alive socket
03-21 21:24:26.407 8035-8035/? V/JSONOBJECT_STRING﹕ {"data":{"id":"gQxwy","title":"playlist","description":null,"datetime":1425872636,"cover":"c7iJOmN","cover_width":160,"cover_height":120,"account_url":"JohnnyJem","account_id":18800283,"privacy":"public","layout":"blog","views":574,"link":"http://imgur.com/a/gQxwy","favorite":false,"nsfw":null,"section":null,"images_count":10,"images":[{"id":"c7iJOmN","title":"Title","description":"Description","datetime":1425872636,"type":"image/gif","animated":true,"width":160,"height":120,"size":65008,"views":1175,"bandwidth":76384400,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/c7iJOmN.gifv","webm":"http://i.imgur.com/c7iJOmN.webm","mp4":"http://i.imgur.com/c7iJOmN.mp4","link":"http://i.imgur.com/c7iJOmN.gif","looping":true},{"id":"SYRfZPy","title":"Title","description":"Description","datetime":1425872638,"type":"image/gif","animated":true,"width":160,"height":120,"size":139574,"views":1054,"bandwidth":147110996,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/SYRfZPy.gifv","webm":"http://i.imgur.com/SYRfZPy.webm","mp4":"http://i.imgur.com/SYRfZPy.mp4","link":"http://i.imgur.com/SYRfZPy.gif","looping":true},{"id":"1aD2tdR","title":"Title","description":"Description","datetime":1425872639,"type":"image/gif","animated":true,"width":160,"height":120,"size":117092,"views":882,"bandwidth":103275144,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/1aD2tdR.gifv","webm":"http://i.imgur.com/1aD2tdR.webm","mp4":"http://i.imgur.com/1aD2tdR.mp4","link":"http://i.imgur.com/1aD2tdR.gif","looping":true},{"id":"7UzUF79","title":"Title","description":"Description","datetime":1425872641,"type":"image/gif","animated":true,"width":160,"height":120,"size":146703,"views":785,"bandwidth":115161855,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/7UzUF79.gifv","webm":"http://i.imgur.com/7UzUF79.webm","mp4":"http://i.imgur.com/7UzUF79.mp4","link":"http://i.imgur.com/7UzUF79.gif","looping":true},{"id":"3PKeSiW","title":"Title","description":"Description","datetime":1425872642,"type":"image/gif","animated":true,"width":160,"height":120,"size":136914,"views":753,"bandwidth":103096242,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/3PKeSiW.gifv","webm":"http://i.imgur.com/3PKeSiW.webm","mp4":"http://i.imgur.com/3PKeSiW.mp4","link":"http://i.imgur.com/3PKeSiW.gif","looping":true},{"id":"o3UBVgR","title":"Title","description":"Description","datetime":1425872644,"type":"image/gif","animated":true,"width":160,"height":120,"size":129046,"views":483,"bandwidth":62329218,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/o3UBVgR.gifv","webm":"http://i.imgur.com/o3UBVgR.webm","mp4":"http://i.imgur.com/o3UBVgR.mp4","link":"http://i.imgur.com/o3UBVgR.gif","looping":true},{"id":"qraDqOQ","title":"Title","description":"Description","datetime":1425872645,"type":"image/gif","animated":true,"width":160,"height":120,"size":93116,"views":466,"bandwidth":43392056,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/qraDqOQ.gifv","webm":"http://i.imgur.com/qraDqOQ.webm","mp4":"http://i.imgur.com/qraDqOQ.mp4","link":"http://i.imgur.com/qraDqOQ.gif","looping":true},{"id":"K9PuB3S","title":"Title","description":"Description","datetime":1425872647,"type":"image/gif","animated":true,"width":160,"height":120,"size":84323,"views":481,"bandwidth":40559363,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":null,"gifv":"http://i.imgur.com/K9PuB3S.gifv","webm":"http://i.imgur.com/K9PuB3S.webm","mp4":"http://i.imgur.com/K9PuB3S.mp4","link":"http://i.imgur.com/K9PuB3S.gif","looping":true},{"id":"p3whCAz","title":"Title","descriptio
03-21 21:24:26.408 8035-8035/? D/AndroidRuntime﹕ Shutting down VM
03-21 21:24:26.408 8035-8035/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.johnnymolina.nextphase, PID: 8035
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.gson.JsonElement.toString()' on a null object reference
at com.johnnymolina.nextphase.activities.Imgur$2.onCompleted(Imgur.java:116)
at com.johnnymolina.nextphase.activities.Imgur$2.onCompleted(Imgur.java:98)
at com.koushikdutta.async.future.SimpleFuture.handleCallbackUnlocked(SimpleFuture.java:107)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:141)
at com.koushikdutta.async.future.SimpleFuture.setComplete(SimpleFuture.java:128)
at com.koushikdutta.ion.IonRequestBuilder$1.run(IonRequestBuilder.java:246)
at com.koushikdutta.async.AsyncServer$RunnableWrapper.run(AsyncServer.java:57)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-21 21:24:26.411 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.Imgur
03-21 21:24:26.412 6534-6864/? W/ActivityManager﹕ Force finishing activity com.johnnymolina.nextphase/.activities.MainActivity
好的看看logCat似乎没有打印出整个JSon对象。这是Log.v可以打印的限制吗?或者我得到一个Null对象引用,因为网络调用没有返回一个完整的对象?
从Imgur的Api中检索出Json对象的示例如下:https://api.imgur.com/models/album
{
"data": {
"id": "lDRB2",
"title": "Imgur Office",
"description": null,
"datetime": 1357856292,
"cover": "24nLu",
"account_url": "Alan",
"account_id": 4,
"privacy": "public",
"layout": "blog",
"views": 13780,
"link": "http://alanbox.imgur.com/a/lDRB2",
"images_count": 11,
"images": [
{
"id": "24nLu",
"title": null,
"description": null,
"datetime": 1357856352,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 855658,
"views": 135772,
"bandwidth": 116174397976,
"link": "http://i.imgur.com/24nLu.jpg"
},
{
"id": "Ziz25",
"title": null,
"description": null,
"datetime": 1357856394,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 919391,
"views": 135493,
"bandwidth": 124571044763,
"link": "http://i.imgur.com/Ziz25.jpg"
},
{
"id": "9tzW6",
"title": null,
"description": null,
"datetime": 1357856385,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 655028,
"views": 135063,
"bandwidth": 88470046764,
"link": "http://i.imgur.com/9tzW6.jpg"
},
{
"id": "dFg5u",
"title": null,
"description": null,
"datetime": 1357856378,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 812738,
"views": 134704,
"bandwidth": 109479059552,
"link": "http://i.imgur.com/dFg5u.jpg"
},
{
"id": "oknLx",
"title": null,
"description": null,
"datetime": 1357856338,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 717324,
"views": 32938,
"bandwidth": 23627217912,
"link": "http://i.imgur.com/oknLx.jpg"
},
{
"id": "OL6tC",
"title": null,
"description": null,
"datetime": 1357856321,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 1443262,
"views": 32346,
"bandwidth": 46683752652,
"link": "http://i.imgur.com/OL6tC.jpg"
},
{
"id": "cJ9cm",
"title": null,
"description": null,
"datetime": 1357856330,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 544702,
"views": 31829,
"bandwidth": 17337319958,
"link": "http://i.imgur.com/cJ9cm.jpg"
},
{
"id": "7BtPN",
"title": null,
"description": null,
"datetime": 1357856369,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 844863,
"views": 31257,
"bandwidth": 26407882791,
"link": "http://i.imgur.com/7BtPN.jpg"
},
{
"id": "42ib8",
"title": null,
"description": null,
"datetime": 1357856424,
"type": "image/jpeg",
"animated": false,
"width": 2592,
"height": 1944,
"size": 905073,
"views": 30945,
"bandwidth": 28007483985,
"link": "http://i.imgur.com/42ib8.jpg"
},
{
"id": "BbwIx",
"title": null,
"description": null,
"datetime": 1357856360,
"type": "image/jpeg",
"animated": false,
"width": 1749,
"height": 2332,
"size": 662413,
"views": 30107,
"bandwidth": 19943268191,
"link": "http://i.imgur.com/BbwIx.jpg"
},
{
"id": "x7b91",
"title": null,
"description": null,
"datetime": 1357856406,
"type": "image/jpeg",
"animated": false,
"width": 1944,
"height": 2592,
"size": 618567,
"views": 29259,
"bandwidth": 18098651853,
"link": "http://i.imgur.com/x7b91.jpg"
}
]
},
"success": true,
"status": 200
}
答案 0 :(得分:1)
如果您可以发布json响应的示例,我们可以帮助您更好。 但是从我所看到的JsonElement“images_count”不存在,所以你得到了NullPointerException。
从我所看到的,如果我尝试从浏览器调用我得到这个json响应:
{"data":{"error":"Authentication required","request":"\/3\/album\/gQxwy.json","method":"GET"},"success":false,"status":401}
您可能不会从WebService添加一些查询参数请求。
P.S。:要显示当前对象/变量值,您需要使用断点并在调试模式下运行应用程序