I have this method in my code
public boolean has(AnyType x){
for(int i=0; i<items.length; i++)
if(items[i] == x)
return true;
return false;
}
and I want to rewrite the if statement to use the compareTo() method, and according to what I remember it should be something like this:
if(items[i].compareTo(x) == 0)
But that is giving me an error
Exception in thread "main" java.lang.NullPointerException
Any ideas of what I might be doing wrong??
NullPointer Exception is thrown when an application attempts to use null in a case where an object is required. Check with your
items[i]values. One of the value must be a null, so that’s why the compiler is throwing aNullPointerException.When you do this, you are trying to compare a null value with
x, which indeed throws aNullPointerException.Do check for the
not-a-null-valuetest. Try this,