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 OverflowYou 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.
When we say nullSafe we usually expect to receive a null from the API when passing null to it, not a 0 or -1 or any other value.
The intention is that the API does not break in case of receiving null, but simply returning null.
For this, use createLong static method of the org.apache.commons.lang3.math.NumberUtils.
CAUSION*:
This throws NumberFormatException if the input String is NOT null and is NOT numeric.
CAUSION**: since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero means octal; spaces are not trimmed.
Sample:
NumberUtils.createLong(null) --> null
NumberUtils.createLong("1") --> 1
NumberUtils.createLong("hi") --> NumberFormatException
NumberUtils.createLong(" 11") --> NumberFormatException (does not trim)
NumberUtils.createLong("023") --> 19 (leading 0 means octal)
NumberUtils.createLong("0x23") --> 35 (leading 0x means hex)
The static valueOf method in the String class will do the null check and return "null" if the object is null:
String stringRepresentation = String.valueOf(o);
Try Objects.toString(Object o, String nullDefault)
Example:
import java.util.Objects;
Object o1 = null;
Object o2 = "aString";
String s;
s = Objects.toString(o1, "isNull"); // returns "isNull"
s = Objects.toString(o2, "isNull"); // returns "aString"
I'm afraid the value is not 1454. The value is null, as evidenced by this line:
java.lang.NumberFormatException: null
You need to check your parameter name, and make sure you've got it right, including case and invisible characters.
java.lang.NumberFormatException: null
If String dxorderId is null then it will throw NumberFormatException
Solution
Check dxorderId is not null and not empty string
if(dxorderId != null && !dxorderId.equals(""))
{
long orderid = Long.parseLong((dxorderId));
}
From oracle docs NumberFormatException
public class NumberFormatException
extends IllegalArgumentException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
Use Long.parseLong()
Copy Long.parseLong("0", 10) // returns 0L
Long.parseLong("473", 10) // returns 473L
Long.parseLong("-0", 10) // returns 0L
Long.parseLong("-FF", 16) // returns -255L
Long.parseLong("1100110", 2) // returns 102L
Long.parseLong("99", 8) // throws a NumberFormatException
Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
Long.parseLong("Hazelnut", 36) // returns 1356099454469L
Long.parseLong("999") // returns 999L
To convert a String to a Long (object), use Long.valueOf(String s).longValue();
See link
here is a solution :
int tryParseInt(String value) {
try {
return Integer.parseInt(value);
} catch(NumberFormatException nfe) {
// Log exception.
return 0;
}
}
you should catch NumberFormatException instead of exception.
I'd consider using NumberUtils.toInt from Apache Commons Lang which does exactly this:
public static int NumberUtils.toInt(java.lang.String str, int defaultValue)
The implementation uses the already mentioned Integer.parseInt with an additional null check.
See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)