[英]Getting error about json and i am new in Android World
I m getting this error when i click a button in Activity im developing a Application in Android studio and getting error about json and im not sure how to handle this error我在 Activity 中单击按钮时收到此错误,我在 Android Studio 中开发应用程序并收到有关 json 的错误,但我不确定如何处理此错误
Error Detail错误详情
Attempt to invoke virtual method 'int org.json.JSONArray.length()' on a null object reference at com.example.tourista.DataParser.getAllnearbyPlaces(DataParser.java:62) at com.example.tourista.DataParser.parse(DataParser.java:100)
尝试在 com.example.tourista.DataParser.getAllnearbyPlaces(DataParser.java:62) 处的 com.example.tourista.DataParser.parse() 处的空对象引用上调用虚拟方法“int org.json.JSONArray.length()”数据解析器.java:100)
private List<HashMap<String,String>> getAllnearbyPlaces (JSONArray jsonArray)
{
//Line There i m getting Error
int counter = jsonArray.length();
List<HashMap<String,String>> NearbyPlacesList = new ArrayList<>();
HashMap<String,String> NearbyplaceMap = null;
if (jsonArray != null) {
for (int i = 0; i < counter; i++) {
try {
NearbyplaceMap = getSingleNearbyPlace((JSONObject) jsonArray.get(i));
NearbyPlacesList.add(NearbyplaceMap);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return NearbyPlacesList;
}
public List<HashMap<String,String>> parse(String JSONdata)
{
JSONArray jsonArray= null;
JSONObject jsonObject;
try
{
jsonObject =new JSONObject(JSONdata);
jsonArray = jsonObject.getJSONArray("results");
}
catch (JSONException e)
{
e.printStackTrace();
}
return getAllnearbyPlaces(jsonArray);
}
Your error is for this line : int counter = jsonArray.length();
你的错误是针对这一行:
int counter = jsonArray.length();
if you have a null object you can't call any method of that如果你有一个空对象,你不能调用它的任何方法
your jsonArray is null so you can't call jsonArray.length()你的 jsonArray 为空,所以你不能调用 jsonArray.length()
you should put int counter = jsonArray.length();
你应该把
int counter = jsonArray.length();
bellow if (jsonArray != null) {
波纹管
if (jsonArray != null) {
For this specific case对于这个特殊情况
jsonArray = jsonObject.getJSONArray("results")
probably throws a JSONException
so your array is never initialized and remains null
. jsonArray = jsonObject.getJSONArray("results")
可能会抛出一个JSONException
所以你的数组永远不会被初始化并保持为null
。 Look in your logs and you will probably see some red lines of text with the error.查看您的日志,您可能会看到一些带有错误的红行文本。
To learn more about Nullpointerexceptions read:要了解有关 Nullpointerexceptions 的更多信息,请阅读:
What is a NullPointerException, and how do I fix it? 什么是 NullPointerException,我该如何解决?
You need to provide more code.您需要提供更多代码。 Who is calling the getAllnearbyPlaces (JSONArray jsonArray)?
谁在调用 getAllnearbyPlaces (JSONArray jsonArray)?
When this class is called you are passing a null JSONArray当这个类被调用时,你传递的是一个空的 JSONArray
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.