這似乎很奇怪,我必須作出檢查,如果對象爲空或空的檢查。Android應用程序崩潰在簡單的if語句
我的代碼:
if(objectList == null || objectList.isEmpty()) {
//Log error here
}
if語句寫這篇如果鏈表類爲空,崩潰說,它不能檢查空的空對象引用後。這是爲什麼發生?
這似乎很奇怪,我必須作出檢查,如果對象爲空或空的檢查。Android應用程序崩潰在簡單的if語句
我的代碼:
if(objectList == null || objectList.isEmpty()) {
//Log error here
}
if語句寫這篇如果鏈表類爲空,崩潰說,它不能檢查空的空對象引用後。這是爲什麼發生?
之前,你可以調用objectList.isEmpty()
,確保你的objectList
不是null
。因此你應該這樣做:
if(objectList != null && !objectList.isEmpty()) {
// not empty
}
else {
// objectList is null or empty
}
乾杯!
試試這個
if(objectList != null) {
if(objectList.isEmpty())
{
//Log here is empty
}
}
else
{
// log here its null
}
查找解決
if((objectList !=null) && (objectList.size()>0)) {
// your implemention
}else{
// your error log here
}
是不是隻是從問題中翻轉出來? –
你可以嘗試趕上
try{
if(objectList == null || objectList.isEmpty()) {
}catch(Exception e){
//print the exception
}
的應用程序不會崩潰
這是一種解決方法,但不是一個好的解決方案。 –
由於問題主要集中在保持應用程序運行。所以它會做的事情。 – Simo
如果它爲空,那麼如何檢查是否爲空 –
這個問題看起來並不像一個可能的問題。 Maibe你使用Instant Run,並且你的代碼在設備上不是最新的? –
@PhanVanLinh,如果objectList爲null,則會導致NPE。 –