ArrayList Null指针使用Condition播放异常

时间:2016-02-20 11:36:54

标签: java android json retrofit

ArrayList NullPointerException (NPE)在为 Google

产品提取数据时

以下是我获得NPE的行:

searchList.add(value); // getting NPE

日志:

02-24 14:25:35.903 17269-17269/app.retrofit_chucknorries E/app.retrofit_chucknorries.MainActivity$2: ERROR: null
02-24 14:25:35.904 17269-17269/app.retrofit_chucknorries W/System.err: java.lang.NullPointerException
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at app.retrofit_chucknorries.MainActivity.searchList(MainActivity.java:132)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at app.retrofit_chucknorries.MainActivity$1.call(MainActivity.java:57)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at app.retrofit_chucknorries.MainActivity$1.call(MainActivity.java:51)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.Observable$32.onNext(Observable.java:7187)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:130)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.internal.operators.NotificationLite.accept(NotificationLite.java:150)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:189)
02-24 14:25:35.909 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.access$000(OperatorObserveOn.java:65)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:153)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:47)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at android.os.Handler.handleCallback(Handler.java:808)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:103)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at android.os.Looper.loop(Looper.java:193)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5299)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at java.lang.reflect.Method.invokeNative(Native Method)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at java.lang.reflect.Method.invoke(Method.java:515)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:829)
02-24 14:25:35.910 17269-17269/app.retrofit_chucknorries W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:645)
02-24 14:25:35.911 17269-17269/app.retrofit_chucknorries W/System.err:     at dalvik.system.NativeStart.main(Native Method)

JSON

{
   "type":"success",
   "value":[
      {
         "id":1,
         "title":"Nexus 9",
         "product":"Google"
      },
      {
         "id":2,
         "title":"iPhone 6S",
         "product":"Apple"
      },
      {
         "id":3,
         "title":"iPhone 5",
         "product":"Apple"
      },
      {
         "id":4,
         "title":"Nexus 7",
         "product":"Google"
      }
   ]
}

现在,我只想fetch data Google

product
public void searchList() {

        if(searchList != null && !searchList.isEmpty()) {
            searchList.clear();
        }

        for (int i = 0; i < valueList.size(); i++) {
            value = (Value) valueList.get(i);

            String strProduct = valueList.get(i).getProduct();

            if (strProduct.equalsIgnoreCase(strSearch)){
                  searchList.add(value); // getting NPE
            }
        }

        mAdapter = new MainAdapter(searchList, R.layout.card_row, getApplicationContext());
        mRecyclerView.setAdapter(mAdapter);

    }

2 个答案:

答案 0 :(得分:2)

看起来您永远不会实例化您要使用的列表。我认为改变这样的方法可以解决您的问题:

public void searchList() {

     if (searchList == null) {
         searchList = new ArrayList<>();
     } else {
         searchList.clear();
     }

     for (int i = 0; i < valueList.size(); i++) {
       ...
     }

     mAdapter = new MainAdapter(searchList, R.layout.card_row, getApplicationContext());
     mRecyclerView.setAdapter(mAdapter);

}

答案 1 :(得分:-1)

您所谈论的searchList仅在变量Object未引用列表null而非List时才会出现。那么,您应该调试 代码并找出searchList 对象 searchList实际上是指null还是not ?如果使用new 关键字在特定的 实例化 {{1}} 。

  

注意: - Dubug您的应用可能会缩短您的问题......