Long.getLong() failing, returning null to valid string
Asked Answered
M

6

42

I've spent the past two hours debugging what seems extremely unlikely. I've stripped the method of a secondary Android Activity to exactly this:

public void onClick(View v) {
        String str = "25";
        long my_long = Long.getLong(str);
} // onClick (v)

And yeah, I get a crash with the good ol' NullPointerException:

09-11 02:02:50.444: ERROR/AndroidRuntime(1588): Uncaught handler: thread main exiting due to uncaught exception 09-11 02:02:50.464: ERROR/AndroidRuntime(1588): java.lang.NullPointerException

It looks like (from other tests) that Long.getLong(str) returns NULL, which is driving me bonkers. WHAT AM I MISSING?

Thanks in advance. I'm okay with stupidly missing the obvious, but my sanity is on the line.

Mur answered 11/9, 2011 at 7:17 Comment(1)
use Long.parseLong(str); instead of Long.getLong(str);Toneme
D
123

You are missing the fact that Long.getLong(String str) is not supposed to parse a String to a long, but rather to return a long value of a system property represented by that string. As others have suggested, what you actually need is Long.parseLong(String str).

Disputable answered 11/9, 2011 at 8:28 Comment(1)
I have often wondered what getLong and the other getX of the primitive types where supposed to do. It seems to me to be a very poor design choice to have getLong on the Long class which is related to system variables or perhaps just a curse to those of us who, like my self, use ctrl+space too extensively. Great response.Burthen
C
12

You can use Long.parseLong(String), instead of getLong(String): it will solve the problem.

Centrobaric answered 11/9, 2011 at 7:20 Comment(0)
B
7

I think you are using wrong function use Long.parseLong(str) then you can get the right answer.

Bypath answered 11/9, 2011 at 7:24 Comment(0)
C
7

Long.parseLong(someString) approved. Don't forget to catch NumberFormatException if there's a probability of unparsable string.

Cringle answered 11/9, 2011 at 7:53 Comment(0)
C
6

To understand this, some examples:

Long val= Long.getLong("32340");

returns: null

Long val = Long.getLong("32340", 3000);

returns: 3000

Using Long.parseLong() :

Long val  = Long.parseLong("32340");

returns: 32340

The documentation describe getLong() method as :

Returns the Long value of the system property identified by string.

this is the code of the getLong() method and only get a property value defined by a string:

  public static Long getLong(String string) {
        if (string == null || string.length() == 0) {
            return null;
        }
        String prop = System.getProperty(string);
        if (prop == null) {
            return null;
        }
        try {
            return decode(prop);
        } catch (NumberFormatException ex) {
            return null;
        }
    }

If you want to parse a String to Long, the best way is using Long.parseLong() method.

Comparison answered 12/5, 2016 at 18:22 Comment(0)
D
2

You can use Long.parseLong(String), instead of getLong(String): it will solve the problem.

Dunstable answered 23/8, 2022 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.