从Android应用程序的Json文件中检索对象时,当我尝试访问Json数组的大小时,我收到NullPointerException。 这是“结果”数组的一个Json对象:
results: [
{
aliases: null,
api_detail_url: "http://www.giantbomb.com/api/game/3030-26559/",
date_added: "2009-05-22 10:03:31",
date_last_updated: "2014-01-14 08:27:36",
deck: "All three Metroid Prime titles in one package. Metroid Prime and Metroid Prime 2: Echoes are reworked to include 480p widescreen, motion controls, and achievements.",
description: "<h2>Overview</h2><p style="">Metroid Prime Trilogy is a single-disc compilation for the <a data-ref-id="3045-36" href="/wii/3045-36/" slug="wii">Nintendo Wii</a> consisting of <a data-ref-id="3030-15473" href="/metroid-prime/3030-15473/" slug="metroid-prime">Metroid Prime</a>, <a data-ref-id="3030-1597" href="/metroid-prime-2-echoes/3030-1597/" slug="metroid-prime-2-echoes">Metroid Prime 2: Echoes</a>, and <a data-ref-id="3030-4725" href="/metroid-prime-3-corruption/3030-4725/" slug="metroid-prime-3-corruption">Metroid Prime 3: Corruption</a>. The first two games in the series, originally released for the <a data-ref-id="3045-23" href="/gamecube/3045-23/" slug="gamecube">Nintendo GameCube</a>, have been updated with 480p widescreen support, improved graphic effects, and motion controls similar to those found in Metroid Prime 3.",
expected_release_day: null,
expected_release_month: null,
expected_release_quarter: null,
expected_release_year: null,
id: 26559,
image: {},
name: "Metroid Prime Trilogy",
number_of_user_reviews: 3,
original_game_rating: [],
original_release_date: "2009-08-24 00:00:00",
platforms: [
{
api_detail_url: "http://www.giantbomb.com/api/platform/3045-36/",
id: 36,
name: "Wii",
site_detail_url: "http://www.giantbomb.com/wii/3045-36/",
abbreviation: "WII"
}
],
site_detail_url: "http://www.giantbomb.com/metroid-prime-trilogy/3030-26559/",
resource_type: "game"
}
我正试图获得“平台”的大小,只是因为可以在PC和MAC上播放游戏:
if (response != null && response.length() > 0) {
Log.d(com.example.ludos2_0.MainActivity.TAG, "Response: "
+ response);
JsonObject jsonObject = (JsonObject) parser.parse(response);
JsonArray items = jsonObject.getAsJsonArray("results");
String id = null;
String title = null;
String thumbnail = null;
String description = null;
String image = null;
String platform = null;
//ArrayList<String> platform = new ArrayList<String>();
for (int index = 0; index < items.size(); index++) {
JsonObject itemObj = (JsonObject) items.get(index);
id = itemObj.get("id").getAsString();
title = itemObj.get("name").getAsString();
thumbnail = ((JsonObject)itemObj.get("image")).
get("tiny_url").getAsString();
image = ((JsonObject)itemObj.get("image")).
get("small_url").getAsString();
description = itemObj.get("deck").getAsString();
JsonArray platforms = jsonObject.getAsJsonArray("platforms");
Log.d(com.example.ludos2_0.MainActivity.TAG,
title);
for (int j = 0; j < platforms.size(); j++) {
JsonObject platformObj = (JsonObject) platforms.get(j);
platform = platformObj.get("name").getAsString();
videogamesList.add(new Videogame(id, title,
thumbnail, image, description, platform));
Log.d(com.example.ludos2_0.MainActivity.TAG,
title + " " + platform);
}
}
但是我在'platforms.size()'的行上得到一个NullPointerException。 我究竟做错了什么? 提前谢谢,对不起我的英语,我是意大利人!
答案 0 :(得分:1)
你没有在这里获得价值
JsonArray platforms = jsonObject.getAsJsonArray("platforms");
platforms
是null
,这就是为什么你会遇到异常
<强>更新强> 我意识到你在这里遇到了问题:
JsonArray platforms = jsonObject.getAsJsonArray("platforms");
更改为:
JsonArray platforms = itemObj.getAsJsonArray("platforms");
答案 1 :(得分:0)
确保您在平台处理程序中获取对象。试试这个:
JsonArray platforms = jsonObject.getAsJsonArray("platforms");
答案 2 :(得分:-1)
您应该从结果中获取平台而不是主对象
JsonArray platforms = jsonObject.getAsJsonArray("platforms"); //WRONG
JsonArray platforms = itemObj.getAsJsonArray("platforms"); //GOOD