开发者社区> 问答> 正文

为什么即使正确设置了返回值非null,也要改造返回null'LiveData'的方法?

我正确地获得了响应,正确设置了LiveData的值(将值设置为实时数据后,通过在控制台上打印值来确认)。但是,当我尝试在“返回”之前打印相同的内容时,它给了我NullPointerException。


public class ProjectRepository {

private ProjectRepository instance;
Context context;
public ProjectRepository(Context context) {
  this.context=context;
}
private MutableLiveData<List<PojoDivision>> data = new MutableLiveData<>();
public LiveData<List<PojoDivision>> getDivisionList() {
    ((RetrofitConfiguration)context).getDivisionRestApiWithAuthentication().getDivisionList().enqueue(new Callback<List<PojoDivision>>() {
        @Override
        public void onResponse(Call<List<PojoDivision>> call, Response<List<PojoDivision>> response) {

            if (response.isSuccessful()) {
                System.out.println(response.body().get(4).getName()); // this is printing

                data.setValue(response.body());
                System.out.println(data.getValue().get(4).getName()); // this is printing
            }
        }

        @Override
        public void onFailure(Call<List<PojoDivision>> call, Throwable t) {
            Log.d(TAG, t.getMessage());
        }
    });
    /*
    following code is not printing with nullPointerException
    java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
     */
    System.out.println(data.getValue().get(4).getName());
    return data;
}
}

展开
收起
Puppet 2020-01-17 09:10:04 397 0
1 条回答
写回答
取消 提交回答
  • 由于您在存储库类的类级别使用LiveData对象,因此您的方法无需返回任何内容。相反,它的责任是更新该LiveData对象。

    
    public class ProjectRepository {
        private static final String TAG = "ProjectRepository";
    
        private ProjectRepository instance;
        Context context;
    
        public ProjectRepository(Context context) {
            this.context = context;
        }
    
        public MutableLiveData<List<PojoDivision>> data = new MutableLiveData<>();
    
        public void getDivisionList() {
            ((RetrofitConfiguration) context).getDivisionRestApiWithAuthentication().getDivisionList().enqueue(new Callback<List<PojoDivision>>() {
                @Override
                public void onResponse(Call<List<PojoDivision>> call, Response<List<PojoDivision>> response) {
    
                    if (response.isSuccessful()) {
                        data.postValue(response.body());
                    }
                }
    
                @Override
                public void onFailure(Call<List<PojoDivision>> call, Throwable t) {
                    Log.d(TAG, t.getMessage());
                }
            });
        }
    
    } 
    

    此类的客户端需要1)观察LiveData对象,以及2)调用该getDivisionList()方法以触发更新:

    class MyFragment extends Fragment {
    
        private ProjectRepository repository;
    
        private void setRepository() {
            Context context = getContext();
            if (context == null) return;
            repository = new ProjectRepository(context);
        }
    
        public void observeData() {
            repository.data.observe(this, new Observer<List<PojoDivision>>() {
                @Override
                public void onChanged(List<PojoDivision> pojoDivisions) {
                    // do something with updated data
                }
            });
        }
    
        public void triggerUpdate() {
            repository.getDivisionList();
        }
    } 
    
    2020-01-17 09:10:33
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
建立联系方法之一 立即下载
fibjs 模块重构从回调到协程--陈垒 立即下载
低代码开发师(初级)实战教程 立即下载

相关实验场景

更多