我已經創建了一個方法,該方法使用遍歷映射的迭代器,並且對於每對來評估具有許多OR條件的語句。如果條件爲真,則它將該對(通知對象)的對象添加到列表(異常)中。但是,在編譯時,編譯器會在此方法中提供NullPointerException異常。根據我的調查,if語句似乎存在問題,但我看不出爲什麼。任何人都可以給我一個幫助嗎?謝謝!if語句中的多個或條件
public List<Notification> getAnomalies(NotificationSearchCriteria notificationSearchCriteria) {
Map<String,Notification> messageList = new HashMap<String,Notification>();
List<Notification> anomalies = new ArrayList<Notification>();
Iterator iterator = messageList.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pairs = (Map.Entry)iterator.next();
Notification message = (Notification) pairs.getValue();
if(message.getDescription().equals(notificationSearchCriteria.getDescription())||message.getSubjectName().equals(notificationSearchCriteria.getSubjectName())||message.getNotificationSubject().toString().equals(notificationSearchCriteria.getNotificationSubject().toString())||message.getNotificationType().toString().equals(notificationSearchCriteria.getNotificationType().toString())){
anomalies.add(message);
}
}
}
return anomalies;
}
了'if'聲明之前,打印出每個對象的您正在檢查('message.getDescription()','notificationSearchCriteria.getDescription() '等),以確保這些都不是「空」。我的猜測是其中之一沒有被分配到某個地方。 – iamnotmaynard 2013-02-08 15:10:58
'NullPointerException'在運行時發生,**從不**在編譯時。 – jlordo 2013-02-08 15:11:36
你的if語句看起來不錯,但對我而言,NullPointerException聽起來像是if語句中的一個對象實際上是null。我會確保notificationSearchCriteria在傳遞給函數時不是null,看看是否有幫助。 – 2013-02-08 15:11:59