-1

I'm looking for the equivalent word in the dabase by the ContextQuery method, and when a equivalent word is null the program must try to use the next index from words and add it up to the current to make it a two word, if the two word is still null the program will make it a three word looking for the next 2 values. And i'm getting a NullPointerException everytime

    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++;
      }
yole
  • 92,896
  • 20
  • 260
  • 197
Untitled
  • 115
  • 2
  • 13

1 Answers1

7

temp == null && temp.isEmpty() can't be right, since if temp is null, temp.isEmpty() will throw NullPointerException.

Either you want to make sure it's null or empty :

temp == null || temp.isEmpty()

Or you want to make sure it's not null and not empty :

temp != null && !temp.isEmpty()

Or you want to make sure it's not null and empty :

temp != null && temp.isEmpty()
Eran
  • 387,369
  • 54
  • 702
  • 768