Reputation: 32321
I have a class as shown
package com;
public class Person {
boolean registered;
public boolean isRegistered() {
return registered;
}
public void setRegistered(boolean registered) {
this.registered = registered;
}
}
The Data to Person Object will be set based on the Data present in DB . The problem is that for older records the registered filed is not present .
so i how can i test if the filed is present or not ??
package com;
public class Test {
public static void main(String args[]) {
Person per = new Person();
if (per.isRegistered()) {
}
}
}
How can i check if the per.isRegistered() field is present or not for that Person Object ??
Upvotes: 3
Views: 27911
Reputation: 288
You should use Optional
if (Optional.ofNullable(per.isRegistered()).orElse(false)) {
// ...
}
Upvotes: 1
Reputation: 41200
boolean
is primitive data type which can not be null, only can be true or false. But Boolean
Object which is rapper of boolean data type, It could be null and Boolean.TRUE
and Boolean.FALSE
;
public class Person {
Boolean registered;
...
}
...
Person per = new Person();
if (per.isRegistered()!=null ) {
if(!per.isRegistered()){
...
}else{
...
}
}
Upvotes: 5
Reputation: 11403
You can use the Boolean
(capital B) class which allows NULL
values for references of its kind.
Alternatively, you can use an enum
having three possible values: NOT_AVAILABLE
, YES
, NO
. This has a clearer semantics and allows you to add other possible values later (if needed).
Upvotes: 1
Reputation: 1756
I don't really get what is on your mind. But if you want to handle a boolean whose values can be one of the following {true/false/null}, you should use a Boolean
object with is a simple boolean
wrapper, but it can take the null
value (as every Java Object can be set to null).
Upvotes: 1
Reputation: 46
You have to extend the person class to use it in the test class. You also have to define the boolean.
Upvotes: 0
Reputation: 5707
Make the column nullable and use a Boolean instead of boolean. For the specific problem though, I think you can assume that if it is null the user is not registered.
Upvotes: 0
Reputation: 5714
boolean registered
as field it's false by default. Moreover boolean
not related to Object
class but primitive value so can't be null
at all.
Only one Null-pointer you may get here it's when per
is null
Upvotes: 0
Reputation: 3238
You need a three state boolean; true, false and not there.
You can achieve this by using Boolean class instead of the primitive boolean and use null as the "not there" indicator.
Upvotes: 1
Reputation: 45083
boolean
can't be null
; you'd need to use (Boolean)
or construct a composite type.
Upvotes: 3
Reputation: 1074188
The way your Person
class is currently defined, you can't, because boolean
(the primitive type) has no null
value, only true
and false
.
You could use Boolean
(the object equivalent), which can be null
, Boolean.TRUE
, or Boolean.FALSE
.
But more likely you want to solve this at a deeper level. You've said you have data in your database for which this field isn't present. Are those records considered registered or not? Your class should reflect the answer to that question.
Upvotes: 6