Home > DeveloperSection > Forums > !array[0].isEmpty() returning NullPointer?
Pooja Malohtra
Pooja Malohtra

Total Post:47

Points:331
Posted on    November-18-2014 10:30 PM

 Android Java  Nullpointerexception 
Ratings:


 1 Reply(s)
 584  View(s)
Rate this:

Here's my code:

if (!quizDescs[0].isEmpty()) {

    mDescText.setText(quizDescs[0]);

} else {

    mDescText.setVisibility(View.INVISIBLE);

}

So, when this code runs, and the if condition returns true, everything is fine and dandy, however, if it returns false, it says there's a NullPointerException, and points me to the line of code containing the if statement.

Am I checking the condition right? Why is it returning a NullPointer?!

ANSWER:

    if (quizDescs[0] == null) {

       mDescText.setVisibility(View.INVISIBLE);

    } else {

       mDescText.setText(quizDescs[0]);

    }



Tom Cruser
Tom Cruser

Total Post:28

Points:196
Posted on    November-19-2014 12:11 AM

if quizDesc[0] is String, you can do

if(!StringUtility.isEmptyOrNull(quizDesc[0])){

 mDescText.setText(quizDescs[0]);

}else {

    mDescText.setVisibility(View.INVISIBLE);

}

By the way, Null and being empty is not same

Consider

String s; //Initialize to null

String a =""; //A blank string

Its always a good practise to use

try{

   //Your code here..

}catch(Exception e){

    e.printStacktrace();

}


Don't want to miss updates? Please click the below button!

Follow MindStick