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 :)

ckng
  • 507
  • 1
  • 4
  • 7
  • You can create class that will check for null values or null object. That will help you improving reuse-ability.. http://stackoverflow.com/a/16833309/1490962 – Bhushankumar Lilapara May 30 '13 at 10:14

3 Answers3

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.

Mike Bockus
  • 2,079
  • 17
  • 23
  • 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. – ckng Mar 23 '12 at 02:34
  • To take advantage of the StringUtils api, you just need to download the commons-lang library from [here](http://commons.apache.org/lang/download_lang.cgi). Download one of the binaries, extract the files, and add the commons-lang3-3.1.jar file to your classpath. – Mike Bockus Mar 23 '12 at 13:14
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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • thanks.but still return the string.I dont know what the problem is. return string example = " Additional label =" – ckng Mar 23 '12 at 02:36
  • @ckng: I don't understand "return the string". I don't see any return statement in your code. Can you please make it clearer? – Bhesh Gurung Mar 23 '12 at 02:38
  • 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? – ckng Mar 23 '12 at 02:51
0

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

DavidB
  • 2,064
  • 3
  • 17
  • 17
  • sorry forget to mention that the string array is getting from the database.hehe.how to check the string value from database is null? – ckng Mar 23 '12 at 02:59
  • 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? – DavidB Mar 23 '12 at 03:15