-
Bug
-
Resolution: Not an Issue
-
P5
-
None
-
8, 25
-
generic
-
generic
OS: Microsoft Windows 11 Business / 10.0.26100 build 26100
Java runtime:
openjdk version "21.0.7" 2025-04-15 LTS
OpenJDK Runtime Environment Microsoft-11369940 (build 21.0.7+6-LTS)
OpenJDK 64-Bit Server VM Microsoft-11369940 (build 21.0.7+6-LTS, mixed mode, sharing)
A DESCRIPTION OF THE PROBLEM :
Writing a method that safetly sums two Numerics (namely Double, Float, Integer and Long), using a ternary operator to return null in case of sum of two nulls, behaves unexpectedly, generating an NPE.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run and compile the given source code
javac SumUtils.java
java SumUtils
EXPECTED VS ACTUAL BEHAVIOR
EXPECTED -
null
ACTUAL -
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Double.doubleValue()" because "<parameter2>" is null
at SumUtils.safeSum(SumUtils.java:9)
at SumUtils.main(SumUtils.java:5)
---------- BEGIN SOURCE ----------
public class SumUtils {
private SumUtils() {}
public static void main(String... args) {
System.out.println(safeSum(null, null));
}
static Double safeSum(Double a, Double b) {
return a == null ? b
: b == null ? a
: a + b;
}
}
---------- END SOURCE ----------
- duplicates
-
JDK-8236521 NullPointerException occurs when using conditional operator with a int and a Map
-
- Closed
-
-
JDK-7029081 NullPointerException from "cond ? : true : false" type operation
-
- Closed
-
-
JDK-7074459 Java compiler allow to return null in a method signature that returns int
-
- Closed
-
-
JDK-7173476 Ternary operator fails on valid statements due to unexpected type casting
-
- Closed
-
-
JDK-8042739 Ternary operator return NullPointerException instead of return null value
-
- Closed
-
-
JDK-8143186 I get NullPointerException when I use long value and null in ternary operator
-
- Closed
-
-
JDK-8163026 javac generates wrong bytecode for "ternary expression" /w java.lang.Integer
-
- Closed
-
-
JDK-8292175 Optimization for ternary conditional operator with boxing cause NPE
-
- Closed
-
-
JDK-8351870 Casting null to Long when using a ternary conditional operator
-
- Closed
-
-
JDK-6211553 Unboxing in conditional operator might cause null pointer exception
-
- Closed
-