Use commons-lang NumberUtils.createInteger() method:

CopyNumberUtils.createInteger(null); // null
NumberUtils.createInteger("5"); // 5
Answer from Maciej Walkowiak on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java numbers › check if an integer value is null or zero in java
Check if an Integer Value Is Null or Zero in Java | Baeldung
January 8, 2024 - Next, let’s test if this works with our three Integer instances: int n0 = 0; boolean result0 = usingTernaryOperator(n0); assertTrue(result0); boolean resultNull = usingTernaryOperator(null); assertTrue(resultNull); int n42 = 42; boolean result42 = usingTernaryOperator(n42); assertFalse(result42); The test passes if we give it a run. So, the usingTernaryOperator() method works as expected. Java 8 introduced the Optional type, so if our Java version is 8 or later, we can also implement this check using a static method from the Optional class:
🌐
LabEx
labex.io › tutorials › java-how-to-utilize-the-integer-valueof-method-in-java-414178
How to utilize the Integer.valueOf() method in Java | LabEx
The Integer.valueOf() method has several important behaviors to consider: Null Input: If the input string is null, the method will throw a NumberFormatException. Whitespace: The method will ignore leading and trailing whitespace characters in ...
Top answer
1 of 4
11

null is not a valid representation of integer number. Integer.parseInt() requires that the string be parsed is a vaild representation of integer number.

Integer.parseInt()

  public static int parseInt(String s, int radix)
440                 throws NumberFormatException
441     {
442         if (s == null) {
443             throw new NumberFormatException("null");
444         }

Integer.valueOf(str)] // which inviokes Integer.parseInt(Str) to return an Integer instance.

  public static Integer valueOf(String s) throws NumberFormatException
569     {
570         return new Integer(parseInt(s, 10));
571     }
2 of 4
9

The folks at Sun who implemented Integer (a long time ago :) ) probably were not thinking of databases when they wrote that method. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. In general, it's a good idea to raise an exception as soon as there is a problem. (a Fail fast design)

If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong.

Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle.

Basically, what you observed is the way it is for now, and someone will have to pay you to code around it :)

🌐
LabEx
labex.io › tutorials › java-how-to-handle-integer-null-comparison-437111
How to handle Integer null comparison | LabEx
This tutorial explores comprehensive ... null pointer exceptions and write more robust code. In Java, null is a special literal that represents the absence of a value....
🌐
Salesforce
trailhead.salesforce.com › trailblazer-community › feed › 0D54S00000A9CK8SAN
Integer.valueOf() ambiguous | Salesforce Trailblazer Community
Skip to main content · Take our 5 minute Community Survey. Open now through November 30th. Click here to participate
Find elsewhere
🌐
BeginnersBook
beginnersbook.com › 2024 › 06 › valueof-method-in-java
ValueOf() Method in Java
In case of String class, String.valueOf(null) returns the string “null” if the input data is null.
🌐
Delft Stack
delftstack.com › home › howto › java › check if int is null java
How to Check if Int Is Null in Java | Delft Stack
February 12, 2024 - Unlike its object-oriented counterparts, int is a primitive data type and is not inherently nullable. However, there are scenarios in which developers may need to handle the absence of a value for an integer-like entity.
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › How-can-I-test-if-a-value-of-INT-type-Not-INTEGER-TYPE-I-m › td-p › 2357049
How can I test if a value of INT-type (Not INTEGER TYPE, I'm talking about INT type) is null?
November 15, 2024 - If it truly needs to be of the int data type, is there a value that could be considered equivalent to null, such as 0 (zero)? If so, you could use something like this: row3.code2 == 0 ? null : row3.code2.toString() ... Ditto - same here! ... The int data type is a primitive data type that is not instantiated from a Java class, unlike Integer.
🌐
javaspring
javaspring.net › blog › how-to-check-whether-an-integer-is-null-or-zero-in-java
How to Check if an Integer is Null or Zero in Java: A Concise Approach (Like StringUtils.isBlank()) — javaspring.net
If the null check were omitted or ordered incorrectly (e.g., integer.intValue() == 0 || integer == null), an NPE would occur for null inputs. Java caches Integer instances for values -128 to 127 (via Integer.valueOf()). However, isNullOrZero works for all Integer instances (including new Integer(0)), as it checks the value, not object identity.
🌐
Baeldung
baeldung.com › home › java › java string › integer.tostring() vs string.valueof() in java
Integer.toString() vs String.valueOf() in Java | Baeldung
April 7, 2025 - Integer.toString() might throw NullPointerException if given Integer object is null. String.valueOf() won’t throw an exception because it’ll go to String.valueOf(Object obj) method and return null.
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-valueof-vs-integer-parseint-with-examples
Integer.valueOf() vs Integer.parseInt() with Examples - GeeksforGeeks
July 11, 2025 - Integer.valueOf(): This method is a static method belonging to the java.lang package which returns the relevant Integer Object holding the value of the argument passed. This method can take an integer or a String as a parameter. But when the given String is invalid, it provides an error.
🌐
W3Docs
w3docs.com › java
How to check if an int is a null
Optional<Integer> optionalNumber = Optional.ofNullable(number); if (optionalNumber.isPresent()) { // number is not null int value = optionalNumber.get(); } else { // number is null }
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java integer valueof() method
Java Integer valueOf() Method
September 1, 2008 - The following example shows the usage of Integer valueOf(String s) method to get the Integer object using the specified String having an int value. We've created a String variable and assigned it a negative int value.