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");
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:
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.
This question already has an answer here:
Chaining of Java Optional map and orElse (if-else-style)
(1 answer)
Closed 4 years ago.
I found myself writing this:
Optional<String> s = fooMaybe.map(Foo::getName).orElse(Optional.empty());
Of course, for the return types to match the compilable way is:
Optional<String> s = fooMaybe.map(foo -> Optional.of(foo.getName)).orElse(Optional.empty());
But is there not a more succinct way? e.g. Does Optional.of(null) return Optional.empty()?
Optional<String> s = Optional.of(fooMaybe.map(Foo::getName).orElse(null));
D'oh. I wasn't realizing that map also returns an Optional of the type being mapped. So all along it was...
Optional<String> s = fooMaybe.map(Foo::getName);
I'll keep this question open for other newbies tripping over their own feet.
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 9 years ago.
i am trying to do a presence check in java on 10 JTextFields. I want it so that if all 10 of my textfield have something in them, it will do my code.
String input1 = tfQ1.getText();
String input2 = tfQ2.getText();
etc.
I have put
IF(input1==("")&&input2==("")&&input3==("")&&input4==("")&&input5==("")&&input6==("")&&input7==("")&&input8==("")&&input9==("")&&input10==(""))
{
//DO SCORES ETC
}
However, this doesnt do anything... (my button does not work weather there are things in the text fields or not)
Please and someone help with presence check validation? Thanks =)
instead of "==" operator you should use
input1.equals("")
if(input.equals("WhateverYouAreLookingFor")) {
//do this
}else {
//do this
}
== is a reference comparison, both objects point to the same location in memory. essentially it tests wether the two operands refer to the same object.
.equals() will only compare what is in the String. It can be overridden so two distinct objects can still be equal.
This question already has answers here:
Best way to "negate" an instanceof
(9 answers)
Closed 2 years ago.
Is it possible to get the opposite of instanceof in java? I have tried code like this:
if( example !instanceof blarg)....
but it won't let me put the ! anywhere without an error, please help.
You have to negate the entire thing:
if(!(example instanceof blarg))
You could also write it like so:
if(example instanceof blarg == false)
As an alternative make use of isInstance method:
if (!example.class.isInstance(blarg))
{
// your code here
}