-1

I have an object S, which contains the method called getstaus(). Now the value of the status coming from the backend could be null also, so I am checking through the following way:

if (S.getStatus().equals(null)) 
{}

Please let me know it is the correct approach or not or is there any other better approach than this.

Lundin
  • 174,148
  • 38
  • 234
  • 367

2 Answers2

11

Your code will throw a NullPointerException if getStatus() returns null, because you're trying to call a method on a reference that is null. Remember: equals() is a method just like any other, so it requires a non-null reference.

You need to use this:

if (s.getStatus() == null) {
  // oh no! the status is null!
}
Joachim Sauer
  • 291,719
  • 55
  • 540
  • 600
1

try checking like this

if(S.getStatus() == null){
 //You will stop here by exception
}
PSR
  • 38,073
  • 36
  • 106
  • 149