I am trying for null check like below
if (isTrue == null)
compile error says : "The operator == is undefined for the argument type(s) boolean"
Please help, how to do null check.
Thanks
You can't do null check on primitive types. boolean is a primitive type.
If you absolutely need to represent a null value with a boolean variable, you need to use the wrapper class java.lang.Boolean.
So, your example would be:
Boolean isTrue;
isTrue = null; // valid
isTrue = true; // valid
isTrue = false; // valid
if (isTrue == null) {
// valid!
}
Here's the WIKIPEDIA entry for primitive wrapper classes.
The right way is
boolean isTrue;
if(!isTrue)
or
if(isTrue)
You can not check if the boolean is null or not.boolean must be true or false.
A boolean is a primative type and cannot be null.
A boolean cannot be null in java.
A Boolean, however, can be null.
Related
can someone explains how this line works in JAVA?
boolean result = value == null
It evaluates value == null, and assigns true or false to result.
Due to the evaluation order, you don't need parentheses result = (value == null).
boolean result = value == null
means you declare a boolean variable result and assigning it to the return value of value == null
value == null --> ture or false
boolean result = true|false
Java will compare the value with null first, after matching both the values then resulting value will be assign as true or false to the result variable. it will check also for data type(if needed, like you cant compare boolean values with null).
if a HashMap is empty and I check for .containsKey()
I get a null answer.
My Problem is that If I want to check for null I get an error message
if(containsKey == null || !containsKey){
I receive the error message
Operator '==' cannot be applied to 'boolean', 'null'
Can someone tell me why this is happening. I thought that this should work
Check that the map isn't null (not that HashMap.containsKey(T) returned null, because it didn't - it can't. It returns a boolean primitive, which can only be true or false).
if (map != null && map.containsKey(someKey)) {
// ...
}
You can use the HashMap .isEmpty() method to check if your hashmap is empty or not.
containsKey can't be null as it is the method being called. Try checking if the map itself is null.
Booleans are primitives, and primitives will never be null.
Only Object classes can be null.
Following this argument, you can do this for object class Integer:
Integer myObject = 1;
if (myObject != null){
...
}
But you cannot do this for int, which is a primitive like booleans:
int myPrimitve = 1;
if (myPrimitve == null){
...
}
Your IDE will show the error Operator == cannot be applied to int, null
When I compile my code this way, I get the mentioned error:
public class SymTree{
public static boolean isSym(BT bt)
{
return(IsMirror(bt.left, bt.right));
}
private static boolean IsMirror(BT lr,BT rr)
{
if(lr==rr==null) (((ERROR HERE)))
return true;
.....
However when I compile like this
private static boolean IsMirror(BT lr,BT rr)
{
if(lr==rr)&&(lr==null))
return true;
.......
I get no error. The error is uncomparable types with nulltype and boolean, however non of my compared objects are boolean- they are both objects from a BT(Binary Tree) class, which has been defined elsewhere.
Thank you!
Examine (lr==rr==null). lr==rr is a boolean. It is primitive and can not be compared to null.
The reason it's giving you that error is because when you write this:
if (lr==rr==null)
The compiler interprets it similar to one of the following:
if ((lr==rr) == null)
if (lr == (rr==null))
Basically, you're comparing a boolean condition (either lr==rr or rr==null) to a nullable type, which doesn't make sense since booleans are value types and can never be null.
It is because in if(lr==rr==null), lr==rr is a boolean comparison which you are comparing with a null by doing ==null.
For Example, if suppose lr is equal to rr then lr==rr will return true next you are comparing whether true==null. Here you get error because boolean and null are not comparable.
Can anybody explain how is it possible that I am getting null pointer exception thrown from this line of code:
if (data != null && data.isActive()) {
Body of method isActive() is just:
public Boolean isActive()
{
return active;
}
Thanks in advance.
In java, there's a thing called autoboxing, when primitive values are wrapped by object types and vice versa.
So, in your code there is a method:
public Boolean isActive()
{
return active;
}
note, that you are returning Boolean (object type), not boolean (primitive type).
and return value is going to be used in your if statement.
if (data != null && data.isActive()) {
when java meets data.isActive() in your if statement, it tries to convert Boolean value to primitive boolean value, to apply it for your logical operation.
But your active variable inside of your isActive() method is null, so java is unable to unbox this variable to boolean primitive value, and you get Null pointer exception.
Can Boolean.valueOf(String) ever return null? From what I can see in the java docs, the docs only specify when it returns true. Is false always returned otherwise, or can null be returned? I have not been able to get it to return null in the tests I have done, but I would like to be sure.
Essentially, I want to know if the following code is safe from a NullPointerException:
boolean b = Boolean.valueOf(...);
The docs pretty much answer it: no. It'll return a Boolean representing a true or false.
The code is also available:
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
No, this is impossible. See the source code of the class Boolean:
public static Boolean valueOf(String s) {
return toBoolean(s) ? TRUE : FALSE;
}
.. and then:
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
Actually it could cause NPE but can not return null. Try this:
Boolean bNull = null;
System.print(Boolean.valueOf(bNull)); // -> NPE!
This happens cuz Boolean.valueOf() accepts String or boolean values. Since bNull is of type Boolean java tries to unbox bNull value to pass it as boolean which causes NPE. Its funny but stupid actually... Also there is no Boolean.valueOf() for Number.
No it will not. If null is placed within the argument or if a string is set to null it will return a boolean value of false. You can see expected inputs and outputs in the Java docs: http://docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html#booleanValue()