2013-02-08 341 views
0

我已經創建了一個方法,該方法使用遍歷映射的迭代器,並且對於每對來評估具有許多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; 
} 
+0

了'if'聲明之前,打印出每個對象的您正在檢查('message.getDescription()','notificationSearchCriteria.getDescription() '等),以確保這些都不是「空」。我的猜測是其中之一沒有被分配到某個地方。 – iamnotmaynard 2013-02-08 15:10:58

+3

'NullPointerException'在運行時發生,**從不**在編譯時。 – jlordo 2013-02-08 15:11:36

+0

你的if語句看起來不錯,但對我而言,NullPointerException聽起來像是if語句中的一個對象實際上是null。我會確保notificationSearchCriteria在傳遞給函數時不是null,看看是否有幫助。 – 2013-02-08 15:11:59

回答

1

這很可能是由於message上的一種方法返回null而引起的。例如,如果message.getDescription()返回null,則message.getDescription().equals(<something>)將拋出NullPointerException,因爲您無法在空對象上調用其他方法。

有幾種方法可以解決這個問題。首先,我建議檢查您的對象,看看哪些可以返回空值並添加適當的處理代碼。

更一般地說,我總是建議在你不知道爲null的變量上調用equals來避免這些問題。例如

if ("accept".equals(command)) { 
    // do something 
} 

通常優於

if (command.equals("accept")) { 
// do something 
} 

因爲通過NPE的第二個可能是,當第一永遠不會。

+0

你說得對,使用字符串文字並在其上調用'equals()'是更安全的方法,但在這種情況下不適用,因爲OP不知道他的''' a.equals(b)'。 – jlordo 2013-02-08 15:22:29

+0

它仍然適用,你只需要更多的代碼知道肯定。例如,NotificationCenter的實現是否保證永不返回null?如果是這樣,請將其用作等號的基礎 – JohnnyO 2013-02-08 15:27:13

0

我會重構消息匹配代碼到NotificationSearchCriteria類。 if最終會成爲「if(notificationSearchCriteria.matches(message))」。從名字來看,我猜測這是NotificationSearchCriteria的唯一用法;在這個意義上說,它不會增加耦合。

檢查零位將在NotificationSearchCriteria施工期間執行;這將確保所有字段都是非空的。在該類中的匹配代碼中,事情將如下所示:

boolean matches(Notification message) { 
    if (description.equals(message.getDescription()) || // LHS guaranteed non-null 
     foo.equals(message.getFoo()) || 
     bar.equals(message.getBar()) || // ... 
    ) { return true; } 
} 
0

代碼的最佳方式是執行空檢查。

理想我想有這樣的代碼:

while (iterator.hasNext()) { 

    Map.Entry pairs = (Map.Entry)iterator.next(); 
    Notification message = (Notification) pairs.getValue(); 
      if(null!=message && null!=message.getDescription() &&   
       null!=notificationSearchCriteria.getDescription()) 
      { 
      //Do your comparioson 
      }else{ 
      //Handle the NullPointerException error the way you want 
      } 
    }