I wrote a realy simple code based on another question and here it is:
It throws me an error
java.lang.NullPointerException line 5 and 17
I don't know what I'm doing wrong.
public class Main {
public static String bool(Boolean param){
if(param == true){ (line 5)
return "a";
}else if(param == false){
return "b";
}
return "c";
}
public static void main(String[] args){
System.out.println(bool(true));
System.out.println(bool(null)); (line 17)
System.out.println(bool(false));
}
}
In Java, the java. lang. NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.
A boolean cannot be null in java. A Boolean , however, can be null . If a boolean is not assigned a value (say a member of a class) then it will be false by default.
What Causes NullPointerException. The NullPointerException occurs due to a situation in application code where an uninitialized object is attempted to be accessed or modified. Essentially, this means the object reference does not point anywhere and has a null value.
It is generally a bad practice to catch NullPointerException. Programmers typically catch NullPointerException under three circumstances: The program contains a null pointer dereference. Catching the resulting exception was easier than fixing the underlying problem.
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. 2. Common places where Java NullPointerException usually occur
Java.lang.Boolean Class in Java. The Boolean class wraps a value of the primitive type boolean in an object. An object of type Boolean contains a single field, whose type is boolean. In addition, this class provides useful methods like to convert a boolean to a String and a String to a boolean, while dealing with a boolean variable.
The compiler interprets null as a null reference to Boolean, applies the autoboxing/unboxing rules for Boolean (on a null) => you get a NullPointerException at runtime. @NilsH probably at the time of writing missed this part of yours. :) +1 from me.
Accessing or modifying a field or data member of the null object. Passing null object as an argument to a method. Calculating the length of a null array. Accessing index of a null array. Synchronizing a null object. Throwing a null object. The Null Pointer Exception extends from the class RuntimeException.
null
cannot be auto-unboxed to a primitive boolean
value, which is what happens when you try to compare it with true
. In
param == true
The type of true
is boolean
, therefore the left-hand operand must also be a boolean
. You are passing in a Boolean
, which is an object, but can be auto-unboxed to boolean
.
Therefore this is equivalent to
param.booleanValue() == true
Clearly, if param
is null
, the above throws NullPointerException
.
To avoid the hidden pitfalls of auto-unboxing, you could instead work with the Boolean
objects:
if (Boolean.TRUE.equals(param))
return "a";
if (Boolean.FALSE.equals(param))
return "b";
return "c";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With