1

I have a jSonObject with key values pairs and i want to parse it to ContentValues. During the parse i want to detect wether the value is null, as you can see in the picture the value is null, but still it does not get catch by the if statement. Because it's a json and might be pasing null as string i tried to use value.equals("null") but neither it worked.

debug info

if ( value == null | value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

What's wrong ?

7
  • value == null | value. equals("null") if value is null not a null string won't value. equals("null") throw a NPE ? I think it should be written as "null".equals(value) Commented Dec 1, 2013 at 16:45
  • Did you print the value of value in console? Try System.out.println("Value:"+value+":"); and show me the exact output. Commented Dec 1, 2013 at 16:50
  • Yes, i did. System.out.println("value is "+value) prints value is null. when it should print value is, that's why i tried the value.equals("null") Commented Dec 1, 2013 at 16:55
  • Try to print the exact line what I have mentioned above and tell me the output Commented Dec 1, 2013 at 16:59
  • Then any one of these condition will be true value == null || value. equals("null"). But it didn't right? Commented Dec 1, 2013 at 17:06

6 Answers 6

3

Your first "OR" is just a single pipe '|' instead of the double-pipe '||'

if ( value == null || value.equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, the OR operator in java can be single pipe or double pipe, if single, and the first statement is true it would not continue checking the next. This way i avoid nullpointer exception.
@Xabier Sorry, that's close but not correct. the | is the bitwise OR operator, not the logical OR operator.
Well, the Sun Java documentation states if both operands are boolean or Boolean then the result of bit-wise operators is boolean, too. That means that a single-pipe bit-wise OR turns into a boolean OR: docs.oracle.com/javase/specs/jls/se7/html/… ... it still doesn't make sense as OP intended. Using bit-wise OR should be as effective at avoiding NPE as boolean OR. Boolean expressions are evaluated from left to right. When you have an OR'ed two part-expression (A OR B) Java will evaluate the whole as true if A is already true.
@netinept I tried but same result. It does not stop. Somehow it is parsing the object null as string, inside the if, i do contentValues.put(columnName, ((value==null)? "": String.valueOf(value))) and it is filling the contentValue as string "null";
1

The common way to retrieve values from jsonobject is using getString() method. So you must use value = jsonObject.getString(columnName); to assign the value for value variable. Then you need to check the condition as

if ( value == null || value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) 
{

}

1 Comment

That made it, what i do not understand yet is why it did not stop in one of both statements (value == null || value.equals("null"), get is supposed to return an object, so it should stop at least on value == null, still, if for some reason it was parsing as string it should stop in value.equals("null") . Anyway, this solution worked like a charm.
1

Try using value.isNullObject() function. It returns true if the object is NULL.

1 Comment

value.isNullObject does not exist !?
1
if ( value == null | value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

Though in your case if value is "null" above will turn out to be

value == null | value. equals("null")// first condition in if statement --->if(false|true)---> if(true)

you are using bit wise OR not logical OR

It should be

  if ( value == null || value. equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
        DO SOME CODE
    }

1 Comment

I tried that as suggested by @netinept but it didn't work. Thanks for your explanation.
1

Try it

if ( value == null || value.equals("null") || tableField.getType() == FIELD_TYPE_NULL ) {
    DO SOME CODE
}

1 Comment

StringUtils is not by default in android studio. I would prefer to avoid adding additional classes. At least just for checking if a value is null. But thanks anyway.
0

You're missing one | in you first condition

1 Comment

Yes, the OR operator in java can be single pipe or double pipe, if single, and the first statement is true it would not continue checking the next. This way i avoid nullpointer exception.

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.