This question already has answers here:
Best way to check for null values in Java?
(19 answers)
Closed 3 years ago.
I want to check if date from database is empty like this:
private boolean hasAccountExpired(LocalDateTime password_changed_at) {
return password_changed_at.isEqual(null);
}
But I get NPE. What is the proper way to check if date field from database is empty?
password_changed_at == null will tell you if it's null.
However, this doesn't necessarily tell you if it's empty in the database - that depends on your implementation.
Related
This question already has an answer here:
Purpose of Objects.isNull(...) / Objects.nonNull(...)
(1 answer)
Closed 12 months ago.
Can someone explain the following code?
if (Objects.nonNull(department.getDepartmentName()) && !"".equalsIgnoreCase(department.getDepartmentName())) {
depDB.setDepartmentName(department.getDepartmentName());
}
The first stab at understanding how a piece of code works should be look at the documentation available.
In your context, code intends to check if department.getDepartmentName() is both not-null and an empty string, only then set it to some value.
On an additional note, double check is redundant.
"".equalsIgnoreCase(department.getDepartmentName())
checks both, if department.getDepartmentName() is not null and is blank string.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
ret_idx might be null for a new user since its entry in database is not made. So whenever the app detects ret_idx is null, it shows the java.lang.NullPointerException. How do I avoid this exception from crashing my app, considering ret_idx might go null in several cases.
String ret_idx=dataSnapshot.child("name").getValue().toString();
Check whether your input data is null or not and if not then assign it to ret_idx.
if(dataSnapshot.child("name").getValue() != null){
String ret_idx=dataSnapshot.child("name").getValue().toString();
}
This question already has answers here:
how to check for an empty array java [duplicate]
(6 answers)
Closed 4 years ago.
In JasperReport I want to print a field when it does not contain empty array.
How can I check it?
In "Print when expression" it is possible to write:
$F{myField} != null
but as $F{myField} is an empty array (so it is not null) it does not work.
Is there any method to check it? It seems to me that there is no isEmpty() function for JasperReport.
Try write something like:
$F{myField}.length() == 0
This question already has answers here:
Avoiding NullPointerException in Java
(66 answers)
Closed 6 years ago.
I have some data in input that I'll have to use to set all properties of a POJO. The POJO might be partially set. My problem is to set the property only if related input data is not null.
I know I can do this in two ways:
if (input != null) {
obj.setData(input);
}
or
obj.setData(input != null ? input : obj.getData());
I'm looking for a solution less ugly and better for objects with a big number of properties to set.
You can also use java8 Optional
obj.setData(Optional.ofNullable(input).orElse(obj.getData()));
Or even use a more elegant way:
Optional.ofNullable(input).ifPresent(obj::setData);
Use guava:
String someString = "value";
Optional.fromNullable(someString).or("defaultValue");
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 7 years ago.
String TypedMaxNumber = MaxValue.getText();
if(TypedMaxNumber == "100")
System.out.println(TypedMaxNumber+" = 100");
I think it is a silly problem, but when I am running to run this program and I type 100 in the text Field it is not going inside the loop. What could be the reason.
I am running to run this program and I type 100 in the text Field it
is not going inside the loop.
Its
if(TypedMaxNumber.equals("100"))
Since TypedMaxNumber is of type String. equals() check for value equality