0

I have a question about how to check either the array string got the null value. My code is like below but still got the string return even the value is null.

for (int i=17;i<29;i++)

{

if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")

{

Log.d("Get additional label","Additional label = "+label[i]);
        }
    }

Problem Solved

The problem solved when I change from

if (!label[i].equals(null) || !label[i].equals("") || label[i] != null || label[i] != "")

to

if (label[i].length() != 0)

Thanks for those who replied :)

1

3 Answers 3

0

You could try using the StringUtils api from the apache commons lib:

if(!StringUtils.isEmpty(label[i]))

Check out the docs for more details.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mike.but I got this error "String Utils cannot be resolved".is it because I need to import something?already import java.lang.Object but still got these error.
To take advantage of the StringUtils api, you just need to download the commons-lang library from here. Download one of the binaries, extract the files, and add the commons-lang3-3.1.jar file to your classpath.
0

Use == or != to check for null.

e.g. label[i] != null and also the way have it now label[i].equals should end up in NPE if it's really null.

3 Comments

thanks.but still return the string.I dont know what the problem is. return string example = " Additional label ="
@ckng: I don't understand "return the string". I don't see any return statement in your code. Can you please make it clearer?
Owh i mean the String return to the LogCat.btw the array string is from the database.is it because the null value from the database is different.how to check the null value from db?
0

Are you sure your string is actually null and not just an empty string?

2 Comments

sorry forget to mention that the string array is getting from the database.hehe.how to check the string value from database is null?
Also don't you want those to be && operators? If you're checking for null in one || operator and your string is empty, you'll end up in your log message and vice versa. Unless that's what you want and I'm misunderstanding?

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.