总是从房间 table 接收空列表
Always receiving null list from room table
我的房间 table 有一些值。我想对其中一些值求和,但出现错误。它在 otherArraySum()
方法中,它是 NullPointerException
。我想知道为什么 otherAmountList
为 null 以及如何解决该问题。提前谢谢你。
查询
@Query("SELECT value FROM statistics_table WHERE category = 7")
LiveData<List<Float>> otherList();
存储库
public Repository(Application application){
AppDatabase database = AppDatabase.getInstance(application);
otherList = statisticsDao.otherList();
}
public LiveData<List<Float>> getOtherList(){
return otherList;
}
视图模型
public class StatisticsViewModel extends AndroidViewModel {
LiveData<List<Float>> otherList;
public StatisticsViewModel(@NonNull Application application) {
super(application);
repository = new Repository(application);
otherList = repository.getOtherList();
public LiveData<List<Float>> getAllOtherList(){
return otherList;
}
}
Activity
List<Float> otherAmountList;
statisticsViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
.getInstance(this.getApplication())).get(StatisticsViewModel.class);
otherAmountList = statisticsViewModel.getAllOtherList().getValue();
otherAmount = otherArraySum();
public float otherArraySum() {
float sum = 0;
for(int i = 0; i < otherAmountList.size(); i++) {
sum = sum + otherAmountList.get(i); }
return sum; }
Logcat
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.moneymanager.MainActivity2.otherArraySum(MainActivity2.java:155)
通过将 activity 代码更改为以下内容解决了我的问题:
float sum;
statisticsViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
.getInstance(this.getApplication())).get(StatisticsViewModel.class);
statisticsViewModel.getAllOtherList().observe(this, new Observer<List<Float>>() {
@Override
public void onChanged(List<Float> floats) {
for(int i = 0; i < floats.size(); i++) {
sum = sum + floats.get(i); }
}
});
我的房间 table 有一些值。我想对其中一些值求和,但出现错误。它在 otherArraySum()
方法中,它是 NullPointerException
。我想知道为什么 otherAmountList
为 null 以及如何解决该问题。提前谢谢你。
查询
@Query("SELECT value FROM statistics_table WHERE category = 7")
LiveData<List<Float>> otherList();
存储库
public Repository(Application application){
AppDatabase database = AppDatabase.getInstance(application);
otherList = statisticsDao.otherList();
}
public LiveData<List<Float>> getOtherList(){
return otherList;
}
视图模型
public class StatisticsViewModel extends AndroidViewModel {
LiveData<List<Float>> otherList;
public StatisticsViewModel(@NonNull Application application) {
super(application);
repository = new Repository(application);
otherList = repository.getOtherList();
public LiveData<List<Float>> getAllOtherList(){
return otherList;
}
}
Activity
List<Float> otherAmountList;
statisticsViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
.getInstance(this.getApplication())).get(StatisticsViewModel.class);
otherAmountList = statisticsViewModel.getAllOtherList().getValue();
otherAmount = otherArraySum();
public float otherArraySum() {
float sum = 0;
for(int i = 0; i < otherAmountList.size(); i++) {
sum = sum + otherAmountList.get(i); }
return sum; }
Logcat
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at com.example.moneymanager.MainActivity2.otherArraySum(MainActivity2.java:155)
通过将 activity 代码更改为以下内容解决了我的问题:
float sum;
statisticsViewModel = new ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory
.getInstance(this.getApplication())).get(StatisticsViewModel.class);
statisticsViewModel.getAllOtherList().observe(this, new Observer<List<Float>>() {
@Override
public void onChanged(List<Float> floats) {
for(int i = 0; i < floats.size(); i++) {
sum = sum + floats.get(i); }
}
});