Java if语句中的多个OR条件
我创建了一个方法,该方法使用迭代器在映射中进行迭代,并为每对映射求值一个包含许多OR条件的语句。如果条件为true,则将该对的对象(通知对象)添加到列表(异常)中。但是,在编译时,编译器在此方法中给出一个NullPointerException异常。根据我的调查,if声明中似乎存在问题,但我不明白为什么。有人能帮我吗?谢谢Java if语句中的多个OR条件,java,Java,我创建了一个方法,该方法使用迭代器在映射中进行迭代,并为每对映射求值一个包含许多OR条件的语句。如果条件为true,则将该对的对象(通知对象)添加到列表(异常)中。但是,在编译时,编译器在此方法中给出一个NullPointerException异常。根据我的调查,if声明中似乎存在问题,但我不明白为什么。有人能帮我吗?谢谢 public List<Notification> getAnomalies(NotificationSearchCriteria notificationSea
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;
}
公共列表获取异常(NotificationSearchCriteria NotificationSearchCriteria){
Map messageList=new HashMap();
列表异常=新建ArrayList();
迭代器迭代器=messageList.entrySet().Iterator();
while(iterator.hasNext()){
Map.Entry pairs=(Map.Entry)迭代器.next();
通知消息=(通知)对。getValue();
如果(message.getDescription().equals(notificationSearchCriteria.getDescription())| message.getSubjectName().equals(notificationSearchCriteria.getSubjectName())| message.getNotificationSubject().toString().equals(notificationSearchCriteria.getNotificationSubject().toString())| message.getNotificationType().toString().equals(notificationSearchCriteria.getNotificationType().toString()){
添加(消息);
}
}
}
回归异常;
}
这很可能是由于消息上的某个方法返回null引起的。例如,如果消息.getDescription()
返回null,则消息.getDescription().equals()
将抛出NullPointerException
,因为您无法对null对象调用其他方法
有几种方法可以解决这个问题。首先,我建议检查您的对象,看看哪些对象可以返回空值,并添加适当的处理代码
更一般地说,我总是建议对不为null的变量调用equals以避免这些问题
if ("accept".equals(command)) {
// do something
}
一般比
if (command.equals("accept")) {
// do something
}
因为第二个可能会通过NPE,而第一个永远不会。我会将消息匹配代码重构为NotificationSearchCriteria
类。if
将最终成为“if(NotificationSearchCriteria.matches(message))”.从名称来看,我猜这是NotificationSearchCriteria
的唯一用法;从这个意义上说,它不会增加耦合
在NotificationSearchCriteria
构造期间将执行null检查;这将确保所有字段均为非null。在该类中的匹配代码中,情况如下所示:
boolean matches(Notification message) {
if (description.equals(message.getDescription()) || // LHS guaranteed non-null
foo.equals(message.getFoo()) ||
bar.equals(message.getBar()) || // ...
) { return true; }
}
编写代码的最佳方法是执行空检查
理想情况下,我会有这样的代码:
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
}
}
在执行if
语句之前,打印出要检查的每个对象(message.getDescription()
,notificationSearchCriteria.getDescription()
,等等)要确保它们都不是null
。我猜其中一个没有被分配到某个地方。NullPointerException
发生在运行时,而不是编译时。if语句看起来没有错,但对我来说NullPointerException听起来像if语句中的一个对象确实是null。我会确保notifica当它被传递到函数中时,并查看是否有帮助。将这个巨大的if
拆分成几个简单的if
语句。这样可以更容易地找到此类错误并维护此类代码。“但是,在编译时,编译器会在此方法中给出NullPointerException异常。”不,不是。我已经有16年没有见过编译器抛出NPE了。你在运行时得到这个,你得到它是因为你取消了对空值的引用。你是对的,使用字符串文字并调用equals()
在上面是更安全的方法,但在这种情况下不适用,因为OP不知道a
或b
在他的a.equals(b)中
。它仍然适用,您只需要更多的代码就可以确定。例如,NotificationCenter的实现是否保证从不返回null?如果是,请将其用作等号的基础