我一直在学习自己编写代码,我真的很困在这里...我正在尝试从Room Database中获取我可以操纵的数据,并以此为基础创建可观察的Live Data列表。这是有问题的代码:
public class Access {
private dao;
private MutableLiveData<List<Element>> currentItem;
private MutableLiveData<List<Element>> nextItem;
private static int cursor = 1;
// THE CONSTRUCTOR
public Access(Application application){
MyDatabase myDatabase = MyDatabase.getInstance(application);
Dao = myDatabase.dao();
try {
currentItem = new GetFirstItem(dao).execute().get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static class GetFirstItem extends AsyncTask<Void, Void, MutableLiveData<List<Element>>> {
List<Element> temporaryResult = new ArrayList<>();
MutableLiveData<List<Element>> result = new MutableLiveData<>();
Dao dao;
boolean loopCheck = true;
String checkedName;
Element checkedElement;
private GetFirstItem(Dao dao) {this.dao =dao;}
@Override
protected synchronized MutableLiveData<List<Element>> doInBackground(Void... voids){
while(loopCheck == true){
checkedElement = dao.getCurrentElement(cursor);
checkedName = dao.getCurrentElementName(cursor);
System.out.println(checkedElement);
System.out.println(checkedName);
if(!checkedName.contains("X")){
temporaryResult.add(checkedElement);
cursor = cursor+1;
System.out.println("DONE ONCE");
}
else{
temporaryResult.add(checkedElement);
cursor = cursor +1;
loopCheck =false;
System.out.println("DONE ONCE");
}
}
result.postValue(temporaryResult);
return result;
}
}
错误:原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'boolean java.lang.String.contains(java.lang.CharSequence)'...
我知道什么是空指针异常...我不知道为什么该值是空的,因为Sysout总是打印一个值...