The easiest way to check is entity == null. There is no shorter way to do that.
Note that there is a method for this in the standard lib:
Objects.isNull(Object obj)
And another one which is the opposite of the above one:
Objects.nonNull(Object obj)
And there is yet another one which can be used to force a value to be not null, it throws a NullPointerException otherwise:
T Objects.requireNonNull(T obj);
Note: The Objects class was added in Java 7, but the isNull() and nonNull() methods were added only in Java 8.
The easiest way to check is entity == null. There is no shorter way to do that.
Note that there is a method for this in the standard lib:
Objects.isNull(Object obj)
And another one which is the opposite of the above one:
Objects.nonNull(Object obj)
And there is yet another one which can be used to force a value to be not null, it throws a NullPointerException otherwise:
T Objects.requireNonNull(T obj);
Note: The Objects class was added in Java 7, but the isNull() and nonNull() methods were added only in Java 8.
try this using reflection
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;
public class ObjectsUtils {
public static boolean allNull(Object target) {
return Arrays.stream(target.getClass()
.getDeclaredFields())
.peek(f -> f.setAccessible(true))
.map(f -> getFieldValue(f, target))
.allMatch(Objects::isNull);
}
private static Object getFieldValue(Field field, Object target) {
try {
return field.get(target);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
Videos
Drawable drawable = Common.getDrawableFromUrl(this, product.getMapPath());
if (drawable == null) {
drawable = getRandomDrawable();
}
The equals() method checks for value equality, which means that it compares the contents of two objects. Since null is not an object, this crashes when trying to compare the contents of your object to the contents of null.
The == operator checks for reference equality, which means that it looks whether the two objects are actually the very same object. This does not require the objects to actually exist; two nonexistent objects (null references) are also equal.
Edited Java 8 Solution:
final Drawable drawable =
Optional.ofNullable(Common.getDrawableFromUrl(this, product.getMapPath()))
.orElseGet(() -> getRandomDrawable());
You can declare drawable final in this case.
As Chasmo pointed out, Android doesn't support Java 8 at the moment. So this solution is only possible in other contexts.
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.
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?