0

I am getting some data from server and setting the values in a getter and setter class. When the values seems to be null i am getting following values in my getter setter class

Tag [seatNo=null, Value=null, player1=null, player2=null, player3=null, player4=null, player5=null ]

The above value is been printed in Logcat

after getting above values i am checking an if condition because of which the app crashes

if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))
{

}

how to check it is null or not ?

3
  • 1
    if(!TextUtils.isEmpty(bet.getSeatNo())) Commented Apr 17, 2015 at 10:48
  • can you please post the error stack trace Commented Apr 17, 2015 at 10:50
  • you are getting a NPE because your bet object is null. What you are trying to do here is ,retrieving a value on a null object resulting in a NPE Commented Apr 17, 2015 at 10:52

4 Answers 4

1

First you need to check if the value is not empty.

if(!TextUtils.isEmpty(bet.getSeatNo())){
//Check your condition here
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try doing this way:

    if (bet.getSeatNo() != null)
    {
        if(!bet.getSeatNo().isEmpty() && !bet.getSeatNo().equals("null"))
        {

        }
    }

Comments

0

Before checking the field empty/null, check whether "bet" object is null or not. NullPointerException occurs while accessing fields of null object.

Comments

0
if (bet.getSeatNo() != null || !bet.getSeatNo().isEmpty() || !bet.getSeatNo().equals("null"))

If bet.getSeatNo() != null is false, only then the second !bet.getSeatNo().isEmpty() is evaluated. But since the first only evaluates to false if getSeatNo() returns null, the second conditional must NPE. Notice the problem?

Use && instead of || so that the short-circuiting stops on first false and not on the first true.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.