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
🌐
Tabnine
tabnine.com › home page › code › java › java.lang.integer
Java Examples & Tutorials of Integer.valueOf (java.lang) | Tabnine
private int retryAfter(Response ... if (header == null) { return defaultDelay; } // https://tools.ietf.org/html/rfc7231#section-7.1.3 // currently ignores a HTTP-date, and assumes any non int 0 is a delay if (header.matches("\\d+")) { return Integer.valueOf(header); } return ...
Find elsewhere
🌐
Coderanch
coderanch.com › t › 674797 › certification › tricky-NullPointerException
That's one very tricky NullPointerException! [Solved] (Associate Certification (OCAJP 8) forum at Coderanch)
And if you are totally flabbergasted by this NullPointerException and you try to rewrite line8 so the conditional expression is not used like this to see what's causing the NullPointerExceptionYou will not get a NullPointerException at all If you want to use the conditional expression without the NullPointerException being thrown, you must use valueOf instead of parseInt (as suggested by Dave, have a cow). So if you replace line8 withthe type of the conditional expression is Integer (both second and third operands evaluate to Integer) and therefor there's no need to unbox defaultValue (which i
🌐
TutorialKart
tutorialkart.com › java › java-string-to-int
Convert String to Int in Java
May 4, 2023 - Not a valid int value."); } catch (NullPointerException e) { System.out.println("Check the string. String is null."); } System.out.print(n); } } You can also use Integer.valueOf() function to convert a string to int. In the following example, we shall use the method valueOf() to get int value from string. Java Program ·
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › integer_valueof_string.htm
Java - Integer valueOf(String s) method
package com.tutorialspoint; public class IntegerDemo { public static void main(String[] args) { String s = "0x170"; System.out.println("String = " + s); /* returns the Integer object of the given string */ System.out.println("valueOf = " + Integer.valueOf(s)); } } Let us compile and run the above program, this will produce the following result − · String = 0x170 Exception in thread "main" java.lang.NumberFormatException: For input string: "0x170" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.valueOf(Unknown Source) at com.tutorialspoint.IntegerDemo.main(IntegerDemo.java:11)