问题描述
我遇到了NullPointerException问题.
I'm having problems with a NullPointerException.
我正在调试,并且元素不为null,我在第一个IF中有问题,看不到是什么问题.我以为这是在元素的状态为null时发生的,但是我试图对其进行修复,但它不起作用
I was doing the debugging and the element is not null, I have the problem in the first IF and I can not see what the problem is. I thought that happened when the state of the element was null, but I tried to fix it and it did not work
//更新:问题是当我检查状态时
// Update: The problem is when I Check the status
for (Items item : master.getItems()) {
try {
if (item.getCompany_status().equals("active") || item.getCompany_status().equals("open")) {
if (item.getAddress_snippet() == null) {
cont2++;
Company c = new Company(item.getTitle(), "");
arrayCompany.add(c);
}else {
cont2++;
Company c = new Company(item.getTitle(), item.getAddress_snippet());
arrayCompany.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
推荐答案
我强烈建议采用其他方式使等式:
I strongly recommend to make the equals the other way:
代替:
item.getCompany_status().equals("active")
做:
"active".equals(item.getCompany_status())
通过这种方式,在运行时,如果状态为null,则在它首先检查字符串时不会崩溃.
This way, at runtime, if the status is null, it won't crash as it first checks the string.
"value".equals(null)
比null.equals("value")
的可能性更大,因为您无法从空对象调用.equals()
.
It is more likely to have "value".equals(null)
than null.equals("value")
as you can't call .equals()
from a null object.
这篇关于Java中的NullPointerException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!