-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
  • 195,001
  • 40
  • 254
  • 396

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
  • 302,674
  • 57
  • 556
  • 614
1

try checking like this

if(S.getStatus() == null){
 //You will stop here by exception
}
PSR
  • 39,804
  • 41
  • 111
  • 151