You can't do it directly, you should provide your own way to check this. Eg.
class MyClass {
Object attr1, attr2, attr3;
public boolean isValid() {
return attr1 != null && attr2 != null && attr3 != null;
}
}
Or make all fields final and initialize them in constructors so that you can be sure that everything is initialized.
Answer from Jack on Stack OverflowYou can't do it directly, you should provide your own way to check this. Eg.
class MyClass {
Object attr1, attr2, attr3;
public boolean isValid() {
return attr1 != null && attr2 != null && attr3 != null;
}
}
Or make all fields final and initialize them in constructors so that you can be sure that everything is initialized.
import org.apache.commons.lang3.ObjectUtils;
if(ObjectUtils.isEmpty(yourObject)){
//your block here
}
[JAVA] is there a way to check if all fields in an object are not empty?
You could use reflection, but I would not recommend using it unless absolutely necessary. You could use streams to make the check a bit more readable, something like
Stream.of(object.name, object.date)
.allMatch(Objects::nonNull);I don't know where you are checking this, but i think this probably should be a method in your class, since this is validator type thingy, not responsibility of the place wherever you add your stuff to a list.
So something like:
public class SomeClass {
String name;
Date date;
public boolean hasAllDetailsEntered() {
return Stream.of(this.name, this.date)
.allMatch(Objects::nonNull);
}
} More on reddit.com ?.let vs if not null - Kotlin Discussions
Cleanest way to check for null on a String?
Fetching a value without having to null check in Java - Software Engineering Stack Exchange
Videos
I wanna add an object to an arrayList and it must have all the fields or else it will not be added.
For example, the fields i need are:
Class(String name, String date, String day)
If the name is null, the class would not be added to my arrayList. Is there a simpler way or do i have to check by:
if (class.name == null || class.date == null || day == null){ System.out.println(" please enter all details") }
^ not sure if class.name is the correct thing, tried it out on eclipse and it doesnt seem to work
You could use reflection, but I would not recommend using it unless absolutely necessary. You could use streams to make the check a bit more readable, something like
Stream.of(object.name, object.date)
.allMatch(Objects::nonNull);
I don't know where you are checking this, but i think this probably should be a method in your class, since this is validator type thingy, not responsibility of the place wherever you add your stuff to a list.
So something like:
public class SomeClass {
String name;
Date date;
public boolean hasAllDetailsEntered() {
return Stream.of(this.name, this.date)
.allMatch(Objects::nonNull);
}
}
Nah, you're doing something wrong. This is probably not necessary. What do you want to do?
Your solution is very smart. The problem I see is the fact that you don't know why you got a null? Was it because the house had no rooms? Was it becuase the town had no houses? Was it because the country had no towns? Was it because there was a null in the 0 position of the collection because of an error even when there are houses in positions 1 and greater?
If you make extensibe use of the NonPE class, you will have serious debugging problems. I think it is better to know where exactly the chain is broken than to silently get a null that could be hiding a deeper error.
Also this violates the Law of Demeter: country.getTown().getHouses().get(0).getLivingRoom(). More often than not, violating some good principle makes you have to implement unorthodox solutions to solve the problem caused by violating such principle.
My recommendation is that you use it with caution and try solve the design flaw that makes you have to incur in the train wreck antipattern (so you don't have to use NonPE everywhere). Otherwise you may have bugs that will be hard to detect.
The idea is fine, really good in fact. Since Java 8 the Optional types exist, a detailed explanation can be found at Java Optional type. A example with what you posted is
Optional.ofNullable(country)
.map(Country::getTown)
.map(Town::Houses);
And further on.
Given the scenario you've given, I would go with implementation A.
In both implementations, the case where house is null or house.getLounge() or house.getLounge().getLetter() return null is handled.
One problem with implementation B is that it treats a NullPointerException that could happen in any of the methods called, which is an abnormal occurrence, as something normal. As commenters have pointed out, it "swallows" exceptions. It does not matter one bit if it can be demonstrated that the as of today, the methods called won't ever raise NullPointerException. Code changes, errors are introduced, what we thought was the case is in fact not the case, etc.
Implementation B also obscures the logic. When I look at implementation A, it is clear that the developer guarded against house being null, etc. I know what set of conditions will cause the "Do nothing..." branch to be taken. In implementation B what is the set of conditions that will result in a NullPointerException? Does getLounge sometimes raise a NullPointerException? I don't know without looking elsewhere.
Another option
import java.util.Optional;
Optional.ofNullable(house)
.map(House::getLounge)
.map(Lounge::getLetter)
.ifPresent(letter -> textView.setText(letter));
This avoids using exceptions for flow control but also avoids verbose handling of each possible null. Instead that logic is part of the optional map logic.