K
K
kirvel2020-10-18 15:35:02
Java
kirvel, 2020-10-18 15:35:02

Error when comparing two strings in Java(Android)?

I'm trying to compare two strings (it would seem: what could be simpler?).

if(!(movie.getLanguage().equals(specifications.language))) {
                return false;
 }

...and I get this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

PS: movie.getLanguage() is not null, as is specifications.language.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2020-10-18
@zagayevskiy

Obviously movie.getLanguage() == null.
Or the error is not in this line.
You can use Objects.equals()

I
ilavio, 2020-10-18
@ilavio

And it is possible to compare through compareTo.

import java.lang.*;
public class StringDemo {
   public static void main(String[] args) {
      String str1 = "tutorials", str2 = "point";
      // компаратор выдает число 0 - если равны +n - если первое больше второго и -n - меньше второго;
      int retval = str1.compareTo(str2);
     
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question