gpt4 book ai didi

java - 解析 JSON 对象时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 18:53:00 25 4
gpt4 key购买 nike

我从下面的代码中得到一个异常:

int[] startApps;
JSONObject jsonObjectMapData=new JSONObject(result);
JSONArray jsonaryPlaceMark = jsonObjectMapData.getJSONArray("Apps");
JSONObject apps= jsonaryPlaceMark.getJSONObject(0); int indx=0;
startApps[indx++]=Integer.parseInt(apps.getString("Thread1"));
startApps[indx++]=Integer.parseInt(apps.getString("Thread2"));

我在线得到 java.lang.NullPointerException:

          startApps[indx++]=Integer.parseInt(apps.getString("Thread1")); 

服务器响应:

{
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
},
"Apps": [ {
"Thread1": 1,
"Thread2": 1,
"Thread3": 1,
"Thread4": 1,
"Thread5": 1,
"Thread6": 1,
"Thread7": 1
} ]
}

最佳答案

您没有初始化数组。

尝试:

int[] startApps;
startApps = int[10]; // or however big it needs to be

或者一步完成:

int[] startApps = int[10];

关于java - 解析 JSON 对象时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15170874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com