從Android的Json文件中檢索對象時,我嘗試訪問Json數組的大小時收到NullPointerException異常。 這裏是「結果」陣列中的一個JSON對象:空指針異常JsonArray大小
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。 我在做什麼錯? 在此先感謝並對我的英語感到抱歉,我是意大利人!
這裏
它不應該是'platforms.length()':試試這個? – florianb
'itemObj.getAsJsonArray(「platforms」)''而不是 – cosmincalistru