问题
ArrayList NullPointerException (NPE) while fetching data for the product of Google only
Here is the line where I am getting NPE:
searchList.add(value); // getting NPE
Log:
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"
}
]
}
Now, I just want to fetch data for the product Google
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);
}
回答1:
It looks like you never instantiate your list to use. I think changing your method like this will fix your issue:
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);
}
回答2:
The Exception that you are talking about only came when it Variable searchList doesn't referring to Object of List rather it refers to null. So, you should debug the code and find out that is List object searchList actually referring to null or not? If it is so Instantiate searchList at that particular line using new Keyword.
Note :- Dubug Your application may short-out your Problem...
来源:https://stackoverflow.com/questions/35522662/arraylist-null-pointer-exception-playing-with-condition