Skip to content
Advertisement

Why the if condition runs the other way

the value passed fromjsonObject.getString("firstName"); to firstNameValidateUser is null as it doesn’t have any value, I need to run the following code which contain String firstName=jsonObject.getString("firstName");.... till returnedUser = new User(firstName, lastName, user.userName, user.password, birthDate, position,qualification,email); when the value of firstNameValidateUser is null. How do I check it,I have used the if condition to check if the firstNameValidateUseris null ,but from the output it seems like it is working the other way round. Is there something wrong with my if condition, or if I have done any other mistake please notify me.. please help me to fix this. Thank you in advance.

firstNameValidateUser=jsonObject.getString("firstName");
// if there are no details are send through the JSON object,
Log.e("jsonObjectlength",jsonObject.length()+"");
Log.e("firstName",firstNameValidateUser);
String usedToCheck=null;
if (firstNameValidateUser!=null && !firstNameValidateUser.isEmpty()) {
    Log.e("firstName","firstName is not null");
    String firstName = jsonObject.getString("firstName");
    String lastName = jsonObject.getString("lastName");
    //String username=jsonObject.getString("username");
    //String password=jsonObject.getString("password");
    String position=jsonObject.getString("position");
    String qualification=jsonObject.getString("qualification");
    String birthDate=jsonObject.getString("birthDate");
    String email=jsonObject.getString("email");
    returnedUser = new User(firstName, lastName, user.userName, user.password, birthDate, position,qualification,email);
    //values are sent to the returnedUser Object
} else {
    Log.e("is Empty","firstName is null");
    returnedUser = null;
}

Advertisement

Answer

The most easiest way to check is using native API

if (jsonObject.isNull("firstName")) {
    Log.e("is Empty","firstName is null");
} else {
    Log.e("firstName","firstName is not null");
}

Refer to android API http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement