Json数组给出了空指针异常

时间:2017-04-11 12:56:16

标签: android arrays nullpointerexception

父布局:
在这里,我在数据对象内创建一个数组。数据插入子部分。

if (((ProxyApplication) mContext.getApplicationContext()).
    getPreferences().
    getStringAttributes(AppConstants.SharePreferencesConstants.SCANNED_STUDENT_LIST_DATA)
    != null)
  try {
    mContext.jsonObject = new JSONObject(((ProxyApplication) mContext.getApplicationContext()).
        getPreferences().
        getStringAttributes(AppConstants.SharePreferencesConstants.SCANNED_STUDENT_LIST_DATA).toString());
    array = mContext.jsonObject.getJSONArray(AppConstants.SharePreferencesConstants.JSON_ARRAY);
    for (int count = 0; count < array.length(); count++) {
      ((ProxyApplication) mContext.getApplicationContext())
          .addScannedStudentList(new ResponseParserManager()
              .parseArrayStudentList(mContext, array.getJSONObject(count)));
    }
  } catch (JSONException e) {
    e.printStackTrace();
  }
else {
  mContext.jsonObject = new JSONObject();
  array = new JSONArray();
  try {
    mContext.jsonObject.put(AppConstants.SharePreferencesConstants.JSON_ARRAY,
        array);
  } catch (JSONException e) {
    e.printStackTrace();
  }
}

子:

Student stObject = new ResponseParserManager().parseResponse(result.getText());
try {

  JSONObject object = new JSONObject(result.getText());
  object.put(AppConstants.SharePreferencesConstants.BRANCH_NAME,
      ((ProxyApplication) mContext.getApplicationContext()).getPreferences()
          .getStringAttributes(AppConstants.SharePreferencesConstants.BRANCH_NAME));
  object.put(AppConstants.SharePreferencesConstants.YEAR,
      ((ProxyApplication) mContext.getApplicationContext()).getPreferences()
          .getStringAttributes(AppConstants.SharePreferencesConstants.YEAR));
  array.put(object);
} catch (JSONException e) {
  e.printStackTrace();
}

这里是array.put我在调试对象显示所有值并且数组被正确实例化但在工作时显示异常后得到空指针的异常。

  

日志:进程:java.lang.NullPointerException:尝试调用虚方法&#39; boolean org.json.JSONArray.isNull(int)&#39;在null对象引用上                                                                                                        at .fragments.ContinousScanFragment $ 1.barcodeResult(ContinousScanFragment.java:114)                                                                                                        在com.journeyapps.barcodescanner.DecoratedBarcodeView $ WrappedCallback.barcodeResult(DecoratedBarcodeView.java:49)                                                                                                        在com.journeyapps.barcodescanner.BarcodeView $ 1.handleMessage(BarcodeView.java:52)                                                                                                        在android.os.Handler.dispatchMessage(Handler.java:98)                                                                                                        在android.os.Looper.loop(Looper.java:135)                                                                                                        在android.app.ActivityThread.main(ActivityThread.java:5343)                                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                                        在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)                                                                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

0 个答案:

没有答案