当我使用sparseArray时崩溃

时间:2019-02-11 19:13:33

标签: android json

我有此代码:

try {
            JSONObject jsonObject = new JSONObject(result);

            String newsInfo = jsonObject.getString("articles");

            JSONArray arr = new JSONArray(newsInfo);

            for(i= 0; i < arr.length();i++){
                JSONObject jsonPart = arr.getJSONObject(i);

                Log.i("source", jsonPart.getString("source"));
                temp = jsonPart.getString("source");
                sparseArraysource.put(i, temp);
                Log.i("author", jsonPart.getString("author"));
                sparseArrayauthor.put(i, temp);
                Log.i("title", jsonPart.getString("title"));
                temp = jsonPart.getString("title");
                sparseArraytitle.put(i, temp);
                Log.i("description", jsonPart.getString("description"));
                temp = jsonPart.getString("description");
                sparseArraydesc.put(i, temp);
                Log.i("url", jsonPart.getString("url"));
                temp = jsonPart.getString("url");
                sparseArrayurl.put(i, temp);
                Log.i("publishedAt", jsonPart.getString("publishedAt"));
                temp = jsonPart.getString("publishedAt");
                sparseArrayat.put(i, temp);
                Log.i("content", jsonPart.getString("content"));
                temp = jsonPart.getString("content");
                sparseArraycontent.put(i, temp);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

我的问题始于sparseArraytitle,我对sparseArraysource和sparseArraysource都没有问题,但是当我尝试将信息存储到sparseArraytitle中时,它就崩溃了。唯一的区别是源和标题只有一个1个单词的字符串,其他则包含更多的单词。

错误日志:

02-12 11:20:00.391 28445-28445/com.example.westbound.sparsearrayprova E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.westbound.sparsearrayprova, PID: 28445
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.util.SparseArray.put(int, java.lang.Object)' on a null object reference
        at com.example.westbound.sparsearrayprova.MainActivity$DownloadTask.onPostExecute(MainActivity.java:104)
        at com.example.westbound.sparsearrayprova.MainActivity$DownloadTask.onPostExecute(MainActivity.java:42)
        at android.os.AsyncTask.finish(AsyncTask.java:727)
        at android.os.AsyncTask.-wrap1(Unknown Source:0)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:744)
        at android.os.Handler.dispatchMessage(Handler.java:108)
        at android.os.Looper.loop(Looper.java:166)
        at android.app.ActivityThread.main(ActivityThread.java:7529)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

我不知道为什么说它具有空值,因为其他人没有这个问题。

0 个答案:

没有答案