I am trying to verify whether an object is null or not and i am using this syntax:
void renderSearch(Customer c){
System.out.println("search customer rendering>...");
try {
if(!c.equals(null)){
System.out.println("search customer found...");
}else{
System.out.println("search customer not found...");
}
} catch (Exception e) {
System.err.println ("search customer rendering error: "
+ e.getMessage()+"-"+e.getClass());
}
}
I get the following exception :
search customer rendering error: null class java.lang.NullPointerException
I thought that I was considering this possibility with my if and else loop. Any help would be appreciated.
Try c != null in your if statement. You're not comparing the objects themselves, you're comparing their references.
!c.equals(null)
That line is calling the equals method on c, and if c is null then you'll get that error because you can't call any methods on null. Instead you should be using
c != null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With