I am using following code to check if the String is empty
if( !Index2.toString().isEmpty()){
....
}
But I run into following error
java.lang.NullPointerException
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your
Index2variable hasnullvalue. That’s why you get the error. Specifically, the error is here:Assuming your
Index2variable is different fromnull, your code will work. In order to make your code to work, you can validate that yourIndex2is different fromnullor add atry/catchblock that handlesNullPointerException.First way:
Second way:
IMHO I would use the first way.