user1448108
user1448108

Reputation: 487

How to check if a string returns null value in android?

For login page in android, I am using php webservice to connect to server database. I stored the response from php service in a string. The response should be either success or failed. But sometimes it is neither returning success or failed. So at that time it is showing null pointer exception. I tried as below but it is showing null pointer exception at line

if (!response.equals(null) && response.equals("SUCCESS"))

when the response is empty. How can I solve this issue. Please help me in this regard.

if (!response.equals(null) && response.equals("SUCCESS")) {
        Intent howis = new Intent(Login.this, Homepage.class);
        startActivity(in);
}
else if (response.equals("FAILED")) {
        new AlertDialog.Builder(Login1.this)
                .setMessage(
                        "Sorry!! Incorrect Username or Password")
                .setCancelable(false).setPositiveButton("OK", null)
                .show();
        password.setText("");
        username.requestFocus();
} else if (response.equals(null)) {
        new AlertDialog.Builder(Login1.this)
            .setMessage("Invalid email or password")
            .setCancelable(false).setPositiveButton("OK", null)
            .show();
        password.setText("");
        username.requestFocus();
} else {
        new AlertDialog.Builder(Login1.this)
            .setMessage("Please Try Again..")
            .setCancelable(false).setPositiveButton("OK", null)
            .show();
        password.setText("");
        username.requestFocus();
}

Upvotes: 0

Views: 6953

Answers (5)

SpongeBobFan
SpongeBobFan

Reputation: 964

You can't use String's methods like equals() when it is null. You should check for null first (response == null). I'd propose to do

if (response == null) {
    //null
} else if (response.equals("SUCCESS")) {
    //success
} else if (response.equals("FAILED")) {
    //failed
} else {
    //neither of those
}

or

if (!response == null && response.equals("SUCCESS")) {
    //success
} else if (!response == null && response.equals("FAILED")) {
    //failed
} else if (response == null) {
    //null
} else {
    //neither of those
}

First way is shorter and less verbose, second has the ordering as your code, what can be better for understanding the code.

Upvotes: 1

Eyal Sooliman
Eyal Sooliman

Reputation: 2248

Another possible workaround (works for me) is to avoid the null pointer exception by setting up a default value inside the layout xml:

android:text="sometext"

That is if your stuck :-)

Upvotes: 0

ridoy
ridoy

Reputation: 6342

You can simply use..

if (!response.equals("") && response.equals("SUCCESS"))
{
...
}

Upvotes: 0

Tarun
Tarun

Reputation: 13808

You can also use

if(TextUtils.isEmpty(response))
{
// response is either null or empty
}

From the docs:

public static boolean isEmpty (CharSequence str)
Returns true if the string is null or 0-length.

Upvotes: 0

Siddharth Lele
Siddharth Lele

Reputation: 27748

If you are checking for an empty (with nothing in it) string, then the condition should be:

if (response == null) {

} else if (response != null) {

}

If you are checking for the String value of null (the String has the value null in it), then the condition should be:

if (response.equals("null")) {

} else {

}

Upvotes: 2

Related Questions