【发布时间】:2016-09-25 17:22:33
【问题描述】:
我正在尝试从 XML 中获取类别列表。我正在使用带有 SimpleXmlConverter 和 RxJavaCallAdapter 的 Retrofit2。获取响应成功,但它包含空对象引用。所以,当我试图将我的列表添加到 RecyclerView 的适配器时,我得到了 NPE。我认为,问题出在 POJO 类中。
这是我的可观察和对应的方法:
restartableLatestCache(GET_CATEGORY_REQUEST, //method from Nucleus MVP library,
() -> api.getCategories("ukAXxeJYZN") //API method, returns Observable<Categories> (see below)
.subscribeOn(Schedulers.io())
.map(Categories::getCategories)
.observeOn(mainThread()),
CategoriesFragment::onItems,
CategoriesFragment::onNetworkError);
void onItems(List<Category> items) {
adapter.add(items); //<- exception raises in this method
}
void onNetworkError(Throwable throwable) {
adapter.hideProgress();
if (throwable instanceof HttpException) {
try {
Log.e(TAG, "Something wrong with your query: " + ((HttpException) throwable).response().errorBody().string() );
} catch (IOException e) {
e.printStackTrace();
}
}
if (throwable instanceof IOException) {
Log.e(TAG, "Network Error or bad conversion: ", throwable.getCause());
}
Log.e(TAG, "Something goes wrong: ", throwable.getCause());
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
//adapter.add(items);
public void add(List<T> items) {
int prevSize = this.items.size();
List<T> list = new ArrayList<>(prevSize + items.size()); <- NPE raises exactly here. items is null, as debugger shows
list.addAll(this.items);
list.addAll(items);
this.items = Collections.unmodifiableList(list);
notifyItemRangeInserted(prevSize, items.size());
}
有一个例外:
FATAL EXCEPTION: main
Process: com.example.testapplication, PID: 26290
java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:112)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: rx.exceptions.OnErrorNotImplementedException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:386)
at rx.internal.util.InternalObservableUtils$ErrorNotImplementedAction.call(InternalObservableUtils.java:383)
at rx.internal.util.ActionSubscriber.onError(ActionSubscriber.java:44)
at rx.observers.SafeSubscriber._onError(SafeSubscriber.java:157)
at rx.observers.SafeSubscriber.onError(SafeSubscriber.java:120)
at rx.exceptions.Exceptions.throwOrReport(Exceptions.java:204)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:144)
at rx.internal.operators.OperatorFilter$FilterSubscriber.onNext(OperatorFilter.java:73)
at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.drain(OnSubscribeCombineLatest.java:286)
at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.combine(OnSubscribeCombineLatest.java:228)
at rx.internal.operators.OnSubscribeCombineLatest$CombinerSubscriber.onNext(OnSubscribeCombineLatest.java:383)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
at rx.internal.operators.OperatorFilter$FilterSubscriber.onNext(OperatorFilter.java:73)
at rx.internal.operators.OperatorMaterialize$ParentSubscriber.onNext(OperatorMaterialize.java:113)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:227)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.testapplication.util.adapters.SimpleListAdapter.add(SimpleListAdapter.java:29)
at com.example.testapplication.ui.cat.CategoriesFragment.onItems(CategoriesFragment.java:74)
at com.example.testapplication.ui.cat.CategoriesPresenter.-com_example_testapplication_ui_cat_CategoriesPresenter-mthref-1(CategoriesPresenter.java:43)
at com.example.testapplication.ui.cat.CategoriesPresenter$-void_onCreate_android_os_Bundle_savedState_LambdaImpl1.call(CategoriesPresenter.java)
at nucleus.presenter.delivery.Delivery.split(Delivery.java:26)
at nucleus.presenter.RxPresenter$4.call(RxPresenter.java:266)
at nucleus.presenter.RxPresenter$4.call(RxPresenter.java:263)
at rx.internal.util.ActionSubscriber.onNext(ActionSubscriber.java:39)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
at rx.internal.operators.OperatorFilter$FilterSubscriber.onNext(OperatorFilter.java:73)
at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.drain(OnSubscribeCombineLatest.java:286)
at rx.internal.operators.OnSubscribeCombineLatest$LatestCoordinator.combine(OnSubscribeCombineLatest.java:228)
at rx.internal.operators.OnSubscribeCombineLatest$CombinerSubscriber.onNext(OnSubscribeCombineLatest.java:383)
at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
at rx.internal.operators.OperatorFilter$FilterSubscriber.onNext(OperatorFilter.java:73)
at rx.internal.operators.OperatorMaterialize$ParentSubscriber.onNext(OperatorMaterialize.java:113)
at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.call(OperatorObserveOn.java:227)
at rx.android.schedulers.LooperScheduler$ScheduledAction.run(LooperScheduler.java:107)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5438)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
你可以看看我的 XML here。
我的 POJO:
@Root(strict = false, name = "categories")
public class Categories {
@Path("yml_catalog/shop")
@ElementList(inline = true, entry="category", type = Category.class)
private List<Category> categories;
//getters, setters
}
@Root(strict = false)
@Path("yml_catalog/shop/categories")
public class Category {
@Attribute
private int id;
@Text
private String categoryName;
//getters, setters
}
那么,我怎样才能修复我的 POJO 以获得正确转换的对象?
【问题讨论】:
-
试一试私有 List
categories = new ArrayList();
标签: java android nullpointerexception retrofit2 simple-framework