Android NullPointerException:尝试调用虚拟方法';int org.json.JSONArray.length();关于空对象引用
我得到了Android NullPointerException:尝试调用虚拟方法';int org.json.JSONArray.length();关于空对象引用,android,nullpointerexception,Android,Nullpointerexception,我得到了NullPointerException:尝试在这行代码的空对象引用上调用虚拟方法“int org.json.JSONArray.length()” for (int i = 0; i < jsonArray.length(); i++) { 你的帮助对我帮助很大 问题是“resultObject.optJSONArray(“quick_questions”)”此值为空 List jsonObject=new ArrayList(); JSONObject resultObjec
NullPointerException:尝试在这行代码的空对象引用上调用虚拟方法“int org.json.JSONArray.length()”
for (int i = 0; i < jsonArray.length(); i++) {
你的帮助对我帮助很大
问题是“resultObject.optJSONArray(“quick_questions”)”此值为空
List jsonObject=new ArrayList();
JSONObject resultObject=null;
JSONArray JSONArray=null;
QuizWrapper newItemObject=null;
试一试{
结果对象=新的JSONObject(结果);
System.out.println(“测试水”+resultObject.toString());
jsonArray=resultObject.optJSONArray(“测验问题”);
}
捕获(JSONException e){
e、 printStackTrace();
}
如果(jsonArray!=null){//检查jsonArray是否为null?
for(int i=0;i
很抱歉,我是初学者,但测验问题是我的表格,该表格有一个值。但结果仍然为null(空):(你应该检查你的代码,找到第一个空对象。先生,它已经在我身上起作用了。非常感谢!你在一个教程(带json解析的android问答应用程序)之后把我从头痛中救了出来,然后我的一个错误被修复了,我已经有两个错误了。非常感谢
private List<QuizWrapper> returnParsedJsonObject(String result) {
List<QuizWrapper> jsonObject = new ArrayList<QuizWrapper>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
QuizWrapper newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("quiz_questions");
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("id");
String question = jsonChildNode.getString("question");
String answerOptions = jsonChildNode.getString("possible_answers");
int correctAnswer = jsonChildNode.getInt("correct_answer");
newItemObject = new QuizWrapper(id, question, answerOptions, correctAnswer);
jsonObject.add(newItemObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
FATAL EXCEPTION: main
Process: androidquizapplication.inducesmile.com.itona, PID: 12093
java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference
at androidquizapplication.inducesmile.com.itona.QuizActivity.returnParsedJsonObject(QuizActivity.java:246)
at androidquizapplication.inducesmile.com.itona.QuizActivity.access$1400(QuizActivity.java:37)
at androidquizapplication.inducesmile.com.itona.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:201)
at androidquizapplication.inducesmile.com.itona.QuizActivity$AsyncJsonObject.onPostExecute(QuizActivity.java:167)
List<QuizWrapper> jsonObject = new ArrayList<QuizWrapper>();
JSONObject resultObject = null;
JSONArray jsonArray = null;
QuizWrapper newItemObject = null;
try {
resultObject = new JSONObject(result);
System.out.println("Testing the water " + resultObject.toString());
jsonArray = resultObject.optJSONArray("quiz_questions");
}
catch (JSONException e) {
e.printStackTrace();
}
if (jsonArray != null) { // check jsonArray is null?
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonChildNode = null;
try {
jsonChildNode = jsonArray.getJSONObject(i);
int id = jsonChildNode.getInt("id");
String question = jsonChildNode.getString("question");
String answerOptions = jsonChildNode.getString("possible_answers");
int correctAnswer = jsonChildNode.getInt("correct_answer");
newItemObject = new QuizWrapper(id, question, answerOptions, correctAnswer);
jsonObject.add(newItemObject);
}
catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}