Why I am getting NullPointerException in my ternary operator?
My ternary operator is throwing a
NullPointerException even though I explicitly check if the value of my list is
null. However, if I add parenthesis around the ternary operator, my code works.
Code without parenthesis (throwing the exception):
final List<String> listData = null; System.out.println("Insert new data (Created Monitorings) : " + listData != null ? listData.size() : 0);
Code with parenthesis (working fine):
final List<String> listData = null; System.out.println("Insert new data (Created Monitorings) : " + (listData != null ? listData.size() : 0));
Can somebody explain it how exactly it is working.
This is about precedence of operators: the String
+ has a much higher precedence than following ternary operator.
Therefore your first snippet actually does this:
( "Insert new data (Created Monitorings) : " + listData ) != null ? listData.size() : 0
Meaning: you concat a string (with a null
listData), and compare that against null. Obviously that concat'ed string isn't null. Thus you do call
listData.size(). And that resembles to
null.size() which leads to your NullPointerException.
You are comparing the wrong thing against null! In the second snippet the ( parenthesises ) ensure that the result of ternary operation gets added to your string.
Why I am getting NullPointerException in my ternary operator , This is about precedence of operators: the String + has a much higher precedence than following ternary operator. Therefore your first snippet� The ternary operator can be used to avoid NullPointerException. First, the Boolean expression is evaluated. If the expression is true then, the value1 is returned, otherwise, the value2 is returned. We can use the ternary operator for handling null pointers:
in snippet one the value that is compared to null is
"Insert new data (Created Monitorings) : " + listData
[JDK-8062801] NullPointerException in ternary operator, NullPointerException in ternary operator. Log In. Export I'm trying to execute such code: The problem is in " value = getCount() == 1 ? As the conditional is true, I would expect the expression of the ternary to be evaluated and the conditional expression skipped. From what I have read in the specifications, this should be the behaviour. Furthermore, I found this question: Java ternary (immediate if) evaluation which should lend some more leverage to my thought process.
'+' has more precedence than '!='
Hence Snippet 1 translates to:
(("Insert new data (Created Monitorings) : " + listData) != null ? listData.size() : 0;
The expression is not null so it proceeds to listData.size(). But as it is null, it throws the exception.
[JDK-8211264] Nullpointer exception using ternary operator and , Using the ternary operator to assign a value to an Integer variable results in a Nullpointer exception. If one part of this operator is a Optional. 1. Why NullPointerException occur in the code. NullPointerException is a situation in code where you try to access/ modify an object which has not been initialized yet. It essentially means that object reference variable is not pointing anywhere and refers to nothing or ‘null‘. A example java program which throws null pointer exception.
Java NullPointerException, Java NullPointerException is an unchecked exception and extends RuntimeException. This operator results to the value on the left hand side if not null else right hand side is You can eliminate messy conditional code if you remember this fact. I am done with my experience around NullPointerException till date. NullPointerException is raised in an application when we are trying to do some operation on null where an object is required. Some of the common reasons for NullPointerException in java programs are; Invoking a method on an object instance but at runtime the object is null. Accessing variables of an object instance that is null at runtime.
Null Pointer Exception In Java, NullPointerException is thrown when program attempts to use an object The ternary operator can be used to avoid NullPointerException. When you compile the code, the compiler generate.class file without any errors. But, when you run the above code, you will get "Exception in thread "main" java.lang.NullPointerException". Here this exception is raised because when you try to access members from a class using object reference that is initialized to null value.
Weird behaviour when using Java ternary operator. When I write my java code like this:
- NullPointerException is not compilation error
- @GhostCat Yes!!! Thank you :)