0

I really don't understand why java.lang.NumberFormatException: Invalid long: "null" happens here.

The problem snippet is like followings.

try {
        userid = ((JSONObject) msg.obj).getString("userid");
        if (userid.equals("") || userid.isEmpty() || null==userid) {
                onClickLogout();
                return;
        } else {
            client.setUserid(Long.parseLong(userid));
       }
    } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            onClickLogout();
            return;
    }

the line client.setUserid(Long.parseLong(userid)); gets following exception

java.lang.NumberFormatException: Invalid long: "null"
at java.lang.Long.invalidLong(Long.java:125)
at java.lang.Long.parse(Long.java:362)
at java.lang.Long.parseLong(Long.java:353)
at java.lang.Long.parseLong(Long.java:319)
at client.setUserid(Long.parseLong(userid));

The point is that the null exception occurs even after null and empty check of the userid in the code. What's wrong with me here? please check it out experts!

5
  • 2
    Check first if the userid is null instead of calling equals otherwise you may run into a NPE Commented Oct 10, 2014 at 14:43
  • I've edited your question, please check. This could avoid a possible NullPointerException. In addition, you should add @laalto 'sanswer. Commented Oct 10, 2014 at 14:49
  • Oh, sorry @PedroOliveira. I've just seen your comment xD Commented Oct 10, 2014 at 14:50
  • Oops, thank you. It was my first edit. I didn't know this. Sorry Commented Oct 10, 2014 at 15:05
  • Thanks guys! The strange thing here is that userid = ((JSONObject) msg.obj).getString("userid"); gets only "" or some numbers. "" case covers that there is no related userid number. the null case is sort of impossible case here actually. and this case happens in rare. I've seen this case just through google report. I'll try @laalto 's solution this time. Thanks! Commented Oct 10, 2014 at 15:39

3 Answers 3

2

The string value and not reference is null.

You can add "null".equals(userid) to test for the literal null value in the if where you check for various forms of "no value".

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

1 Comment

Thanks guys! The strange thing here is that userid = ((JSONObject) msg.obj).getString("userid"); gets only "" or some numbers. "" case covers that there is no related userid number. the null case is sort of impossible case here actually. and this case happens in rare. I've seen this case just through google report. I'll try your solution this time. Thanks!
2

Always check first for null value in your condition

if (null==userid || userid.isEmpty() || userid.equals("null") {

Then, in the next element of your condition, you are sure that userid is not null and no NullPointerException will be thrown

Also, in your case, check that userid does not contain the "null" String

Moreover userid.equals("") and userid.isEmpty() are the same thing.

1 Comment

Thanks for your comment! laalto stole your thunder!
0

You can check null with utility method isEmpty from TextUtils,

public static boolean isEmpty(CharSequence str) {
     return str == null || str.length() == 0;
}

isEmpty(CharSequence str) method check both condition, for null and length.

Comments

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.