2

am presently trying to get a response from a server which contains a response code of "00,02" i used an if and else if statement to perform an action for each of this response but am getting null pointer for the first if statement

if (str.equalsIgnoreCase("02")){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}else if(str.equalsIgnoreCase("00")){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}else{
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
henry
  • 31
  • 1
  • 7
  • It means, `str` is null. How are you assigning value to `str`? – Rohit5k2 Jun 05 '16 at 17:42
  • am getting the values directly from a json response, if the response does not have a responseCode then am to toast the response message out to the user, that is what the last else is meant to be for – henry Jun 05 '16 at 19:52
  • In that case your first check should be for null. Check my answer. – Rohit5k2 Jun 05 '16 at 19:58
  • You might want a unique message for each case, if the code is to do anything useful... – Chris Stratton Jun 05 '16 at 20:19
  • I'd recommend you change the title, it's quite misleading and very vague. – Vucko Jun 05 '16 at 20:20
  • @njzk2 - don't close a question with answers which usefully address its **specific issue** as a duplicate of a generic one which only provides broad advice, **especially when the poster had already done what was advised there** by specifying the exact location of the NPE even from the original posted form of their question. – Chris Stratton Jun 05 '16 at 21:30

3 Answers3

6

A null-safe way of comparing strings would be:

if ("02".equalsIgnoreCase(str)) {
    ...
}

So just flip the strings in each of your conditions and you should be good to go!

Marcin Koziński
  • 10,835
  • 3
  • 47
  • 61
0

Update your condition like this

if(str == null || str.length() == 0){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
else if (str.equalsIgnoreCase("02")){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}else if(str.equalsIgnoreCase("00")){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
else{
    // Unknown code from server
}
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
0

You can use the TextUtils.isEmpty(str) function like so:

if (!TextUtils.isEmpty(str)){

    if (str.equalsIgnoreCase("02")){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }else if(str.equalsIgnoreCase("00")){
        Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }else{
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
    }
}

This function basically does this for you:

public static boolean isEmpty(@Nullable CharSequence str) {
    if (str == null || str.length() == 0)
        return true;
    else
        return false;
}

Which you could've done yourself, but it might be clearer this way.

Vucko
  • 7,371
  • 2
  • 27
  • 45