You can use the NumberUtils from Apache Commons. It's null-safe and you can optionally specify a default value.

Example:

NumberUtils.toLong(null) = 0L
NumberUtils.toLong("")   = 0L
NumberUtils.toLong("1")  = 1L

NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L)   = 1L
NumberUtils.toLong("1", 0L)  = 1L

For more info, check the API.

Answer from Ehler on Stack Overflow
🌐
TutorialKart
tutorialkart.com › java › java-string-to-long
Convert String to Long in Java
January 9, 2023 - Just like Long.parseLong(), the function Long.valueOf() also throws NullPointerException if the string argument is null, or a NumberFormatException if the string does not parse to a valid long value.
🌐
Java67
java67.com › 2015 › 07 › how-to-parse-string-to-long-in-java.html
How to convert String to long in Java? Example | Java67
Just keep in mind that long and ... automatically convert primitive to object and object to primitive, it can also result in NullPointerException if object is null....
🌐
Don't Panic!
dontpanicblog.co.uk › home › parselong vs parsedouble
parseLong vs parseDouble - Don't Panic!
February 21, 2023 - long parseOrDefault(String val, long def) { try { return Long.parseLong(val); } catch (NumberFormatException e) { return def; } } float parseOrDefault(String val, float def) { try { return Float.parseFloat(val); } catch (NumberFormatException e) { return def; } } double parseOrDefault(String val, double def) { try { return Double.parseDouble(val); } catch (NumberFormatException e) { return def; } } ... PARSABLE = "42"; UNPARSABLE = "forty-two"; NULL = null; System.out.println("Parsing: " + PARSABLE); System.out.println(parser.parseOrDefault(PARSABLE, -1)); System.out.println(parser.parseOrDefa
🌐
BeginnersBook
beginnersbook.com › 2015 › 05 › java-long-to-string
Java long to String Conversion
November 20, 2022 - If the long value is null, the String contains "null" after conversion, instead of throwing NullPointerException. We can use String.valueOf(long l) method to convert long to String. This method takes a long value as an argument and returns a string representation of it.
🌐
Tabnine
tabnine.com › home › code library
Code Library - Tabnine
July 25, 2024 - The Tabnine Code Library is no longer available — but you can still get the answers and suggestions you need from our AI code assistant.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java string › convert string to long or long in java
Convert String to long or Long in Java | Baeldung
January 10, 2024 - It uses Long.decode() under the hood with one important addition – if the String argument is null, then it returns null. Now, let’s suppose we have a String which represents a value outside of the signed range of the long primitive.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Long.html
Long (Java Platform SE 7 )
The second argument is the default value. A Long object that represents the value of the second argument is returned if there is no property of the specified name, if the property does not have the correct numeric format, or if the specified name is empty or null.
🌐
LabEx
labex.io › tutorials › java-how-to-ensure-the-safety-and-robustness-of-long-to-string-conversion-in-java-414020
How to ensure the safety and robustness of long to string conversion in Java | LabEx
Test various scenarios, including null values, extreme values, and error conditions, to validate the correctness and reliability of your implementation. By following these best practices and recommendations, you can ensure the safety and robustness of long-to-string conversion in your Java ...
🌐
GitHub
github.com › rapidsai › cudf › issues › 5110
[BUG] Cast from string to long should return null if string is not a valid long · Issue #5110 · NVIDIA/cudf
May 6, 2020 - Casting a string to long where the string is not a valid long in the range Long.MIN_VALUE to Long.MAX_VALUE should return null rather than overflow.
Author: NVIDIA
🌐
Java67
java67.com › 2015 › 10 › java-how-to-convert-from-integer-to-String-in-Java.html
3 ways to Convert Integer to String in Java [Example] | Java67
This is the best and straightforward way to convert an Integer to a String object in Java. It doesn't require any auto-unboxing etc, but it will throw NullPointerException if Integer is null as shown below:
🌐
Stack Overflow
stackoverflow.com › questions › 76514512 › null-safe-integer-longvalue-in-java
Null-safe Integer.longValue() in Java? - Stack Overflow
Especially when value is not just a value but is actually some function with long name and multiple arguments. Maybe I have missed something and there is such a method in any of the popular libraries? ... Java generally doesn't do "short and convenient". I recommend upgrading to Kotlin, which has null-safety built in: intValue?.toLong().
🌐
Baeldung
baeldung.com › home › java › java string › convert long to string in java
Convert Long to String in Java | Baeldung
July 22, 2026 - Be careful because if obj is null, we’ll get an IllegalArgumentException. In summary, we’ve learned different ways to convert Long to String in Java.
🌐
Quora
quora.com › In-Java-how-do-you-check-a-long-for-null
In Java, how do you check a long for null? - Quora
Answer (1 of 11): a “long” is a scalar type, so it cannot be null. You’ll get a NullPointerException. If you mean “Long” the object, then the test is just like anything else “myLongObject == null