我正在通过ContextQuery方法在dabase中查找等效单词,当等效单词为null时,程序必须尝试使用单词中的下一个索引并将其添加到当前以使其成为两个单词,如果两个单词仍为空,程序将使它成为一个三个字,寻找下两个值。而且我每次都会收到NullPointerException
for (int i = 0; i < words.size(); i++)
{
try {
temp = QueryWithContext.query(words.get(i));
if(temp == null && temp.isEmpty() && words.size() >= i+1)
{
QueryWithContext.query(words.get(i)+" "+words.get(i+1));
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1));
if(temp == null && temp.isEmpty());
{
temp = words.get(i);
}
}
else if(temp == null && temp.isEmpty() && words.size() >= i+2)
{
temp = QueryWithContext.query(words.get(i)+" "+words.get(i+1)+" "+words.get(i+2));
if(temp == null && temp.isEmpty());
{
temp = words.get(i);
}
}
System.out.println(temp);
holder = holder +" "+ temp;
counter++;
}
答案 0 :(得分:7)
temp == null && temp.isEmpty()
不对,因为如果temp
为空,temp.isEmpty()
会抛出NullPointerException
。
要么确保它为空还是空:
temp == null || temp.isEmpty()
或者你想确保它不是空的而不是空的:
temp != null && !temp.isEmpty()
或者您想确保它不为空且为空:
temp != null && temp.isEmpty()