引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONArray.length()'[duplicate]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONArray.length()'[duplicate]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我需要从json
解析String builder
,但我收到此错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
我不知道为什么stringBuilder.toString()
给我带有物体的数组!
这是我的代码:
try {
// HttpResponse is an interface just like HttpPost.
//Therefore we can't initialize them
HttpResponse httpResponse = httpClient.execute(httpPost);
// According to the JAVA API, InputStream constructor do nothing.
//So we can't initialize InputStream although it is not an interface
InputStream inputStream = httpResponse.getEntity().getContent();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String bufferedStrChunk = null;
while((bufferedStrChunk = bufferedReader.readLine()) != null){
stringBuilder.append(bufferedStrChunk);
}
// return stringBuilder.toString();
System.out.println("EHEH"+stringBuilder.toString());
JSONArray mainObject = null;
try {
mainObject = new JSONArray(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < mainObject.length(); i++) {
JSONObject object = null;
try {
object = mainObject.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
try {
Video video=new Video();
String url = object.getString("url");
String titolo = object.getString("titolo");
String sottotitolo = object.getString("sottotitolo");
String data = object.getString("date");
//video.setId(idd4);
System.out.println("CAZZO:"+video.getId());
video.setPic(url);
video.setTitolo(titolo);
video.setSottotitolo(sottotitolo);
video.setData(data);
videoList.add(video);
} catch (JSONException e) {
e.printStackTrace();
}
}
有人能帮我吗?
JSON响应:
答案
你的错误是String data = object.getString("data");
JSONObject mainObject = null;
try
{
mainObject = new JSONObject(stringBuilder.toString());
JSONArray jsonArrayPDF = mainObject.getJSONArray("pdfs");
for (int i = 0; i < jsonArrayPDF.length(); i++) {
JSONObject object = jsonArrayPDF.getJSONObject(i);
Video video=new Video();
String url = object.getString("url");
String titolo = object.getString("titolo");
String sottotitolo = object.getString("sottotitolo");
String data = object.getString("data");
//video.setId(idd4);
System.out.println("CAZZO:"+video.getId());
video.setPic(url);
video.setTitolo(titolo);
video.setSottotitolo(sottotitolo);
video.setData(data);
videoList.add(video);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
另一答案
stringBuilder.toString()
回归了什么?使用System.out.println()
另一答案
你忘了初始化StringBuilder
。
首先初始化然后再使用
StringBuilder sb = new StringBuilder();
以上是关于引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'int org.json.JSONArray.length()'[duplicate]的主要内容,如果未能解决你的问题,请参考以下文章
Sqoop - 尝试连接到 Oracle DB 服务器时出现 java.lang.NullPointerException
如何区分两个“onpause”事件 - 由单击“暂停”按钮引起,以及由到达媒体片段末尾引起?