简体   繁体   English

收到关于 json 的错误,我是 Android 世界的新手

[英]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.

相关问题 任何人都可以帮助我了解在 Android 中使用 AsyncTask 时出现错误代码 400 - Can anyone help me about I am getting error code 400 while working with AsyncTask in Android Libgdx-输入处理[Android]。 我对听众不了解? - Libgdx - Input Processing [Android]. What am I not getting about Listeners? 为什么在Java中出现有关Windows文件路径的错误? - Why am I getting an error about a Windows file path in Java? 我收到关于我的索引超出范围的错误 - I am getting an error about my index being out of bounds 为什么我收到关于compareTo方法的此错误? - Why am I getting this error about the compareTo method? 在 Android 上启动 Appium 时出现错误 - I am getting an error when I am launching Appium on Android 尝试解析给定的json格式的android时,解析给定的值时出现错误 - I am getting error parsing the given value when tried to parse the given json format android 我正在尝试使用 servlet 做简单的 hello world 程序。 但我收到错误 HTTP 状态 405 - 不允许的方法 - I am trying to do simple hello world program using servlets. But i am getting an error HTTP status 405 - Method not allowed 我正在研究 API 并在 android 中获得 JSON 的结果 - I am working on API and getting result in JSON in android 你好。 我是 android 应用程序开发的新手。 我尝试创建一个简单的计数器,但在构建后出现错误“无法实例化活动” - Hi! I am new to android app development. I tried to create a simple counter but after build getting error “Unable to instantiate activity”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM