在for循环中迭代字符串时出现空指针异常

时间:2014-05-14 10:12:38

标签: android arrays json

我有一个类似

的安卓代码
JSONObject json = new JSONObject(result);

                JSONArray articles = json.getJSONArray("images");

                for(int i=0;i<json.getJSONArray("images").length();i++){

                image_first = (articles.getJSONObject(i).optString("image_first"));
                image_second = (articles.getJSONObject(i).optString("image_second"));
            }

解析JSON数组。所以现在我想将结果存储到单独的数组中,如

image_first[i] = (articles.getJSONObject(i).optString("image_first"));
                image_second[i] = (articles.getJSONObject(i).optString("image_second"));

但是当我这样做时会抛出nullpointer异常这里的问题是什么,任何帮助都会赞赏

json array

logcat的

05-14 15:51:43.641: E/AndroidRuntime(740): FATAL EXCEPTION: main
05-14 15:51:43.641: E/AndroidRuntime(740): java.lang.NullPointerException
05-14 15:51:43.641: E/AndroidRuntime(740):  at com.kod.manju_app.MainActivity$HttpAsyncTask.onPostExecute(MainActivity.java:123)
05-14 15:51:43.641: E/AndroidRuntime(740):  at com.kod.manju_app.MainActivity$HttpAsyncTask.onPostExecute(MainActivity.java:1)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.os.AsyncTask.finish(AsyncTask.java:417)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.os.Looper.loop(Looper.java:123)
05-14 15:51:43.641: E/AndroidRuntime(740):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 15:51:43.641: E/AndroidRuntime(740):  at java.lang.reflect.Method.invokeNative(Native Method)
05-14 15:51:43.641: E/AndroidRuntime(740):  at java.lang.reflect.Method.invoke(Method.java:521)
05-14 15:51:43.641: E/AndroidRuntime(740):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-14 15:51:43.641: E/AndroidRuntime(740):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-14 15:51:43.641: E/AndroidRuntime(740):  at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案