I need to make sure that no object attribute is null and add default value in case if it is null. Is there any easy way to do this, or do I have to do it manually by checking every attribute by its getters and setters?
-
Short answer is that you have to do it manually, here's a similar question: http://stackoverflow.com/questions/997482/does-java-support-default-parameter-values – Upgradingdave Jan 27 '10 at 10:46
-
1Can you provide a bit more context to your problem. There are a few ways you could do this. You could use AOP to wrap your getters and return default values if they return null. Or you could just write some code to wrap your objects in a dynamic proxy that does essentially the same thing. With some more info we can choose an appropriate option. – Shane Bell Jan 27 '10 at 10:52
-
with java-8, there is another way [too](https://stackoverflow.com/a/61033202/1059372) – Eugene Apr 04 '20 at 20:03
8 Answers
You can use reflection to iterate over the object's field, and set them. You'd obviously need some sort of mapping between types or even field names and required default values but this can be done quite easily in a loop. For example:
for (Field f : obj.getClass().getFields()) {
f.setAccessible(true);
if (f.get(obj) == null) {
f.set(obj, getDefaultValueForType(f.getType()));
}
}
[Update]
With modern Java, you can use annotations to set the default values for fields on a per class basis. A complete implementation might look like this:
// DefaultString.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface DefaultString {
String value();
}
// DefaultInteger.java:
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface DefaultInteger {
int value();
}
// DefaultPojo.java:
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
public class DefaultPojo {
public void setDefaults() {
for (Field f : getClass().getFields()) {
f.setAccessible(true);
try {
if (f.get(this) == null) {
f.set(this, getDefaultValueFromAnnotation(f.getAnnotations()));
}
} catch (IllegalAccessException e) { // shouldn't happen because I used setAccessible
}
}
}
private Object getDefaultValueFromAnnotation(Annotation[] annotations) {
for (Annotation a : annotations) {
if (a instanceof DefaultString)
return ((DefaultString)a).value();
if (a instanceof DefaultInteger)
return ((DefaultInteger)a).value();
}
return null;
}
}
// Test Pojo
public class TestPojo extends DefaultPojo {
@DefaultString("Hello world!")
public String stringValue;
@DefaultInteger(42);
public int integerValue;
}
Then default values for a TestPojo
can be set just by running test.setDetaults()

- 30,470
- 17
- 104
- 128
-
7I'd suggest to use getDeclaredFields instead of getFields - worked for me, because declaredFields includes private fields. – ZZ 5 Jul 09 '14 at 10:40
-
1That is true. Normally, I'd expect this behavior to be used for data objects only (POJOs), so `getFields()` should suffice and will probably even be the "Right Thing", but if the requirements do call for also initializing private fields then `getDeclaredFields()` should be used. – Guss Jul 09 '14 at 11:52
-
-
Could you please guide here: https://stackoverflow.com/questions/61032842/java-8-to-remove-string-value-from-each-field-where-string-value-comes-of-the ? – PAA Apr 04 '20 at 18:28
You need to manually filter input to constructors and setters. Well... you could use reflection but I wouldn't advise it. Part of the job of constructors and setters is to validate input. That can include things like:
public void setPrice(double price) {
if (price < 0.0d) {
throw new IllegalArgumentException("price cannot be negative " + price);
}
this.price = price;
}
and
public void setName(String name) {
if (name == null) {
throw new NullPointerException("name cannot be null");
}
this.name = name;
}
You could use wrapper functions for the actual check and throwing the exception.

- 616,129
- 168
- 910
- 942
-
Yeah, just specify default values when introducing the variables and/or in the constructor and then in each setter guard against passing in null. – Petri Pellinen Jan 27 '10 at 10:47
-
5I won't throw a `NullPointerException` in case of `null` argument : it doesn't give any hint. I'll throw `IllegalArgumentException` in every case and let the JVM throw `NullPointerException` itself when it enconters one. I aggree not to use reflexion. – Jean-Philippe Caruana Jan 27 '10 at 11:09
-
See http://stackoverflow.com/questions/3881/illegalargumentexception-or-nullpointerexception-for-a-null-parameter for a discussion on this. Personally, I prefer to use `InvalidArgumentException` in both cases. – Pascal Thivent Jan 27 '10 at 11:17
-
-
1Let's not turn this into a `NullPointerException` vs `IllegalArgumentException` debate. That's been done to death. This ie neither the time nor the place. – cletus Jan 27 '10 at 11:51
-
or with method handles [like this](https://stackoverflow.com/a/61033202/1059372) – Eugene Apr 04 '20 at 20:04
Non-reflective solution for Java 8, without using a series of if's, would be to stream all fields and check for nullness:
return Stream.of(id, name).allMatch(Objects::isNull);
This remains quite easy to maintain while avoiding the reflection hammer. This will return true for null attributes.

- 840
- 1
- 11
- 15
-
is this optimized to perform as efficiently as a series of `if`s during runtime? – Woodrow Barlow Jan 04 '18 at 20:45
-
-
@shmosel because this (at least the way its used here) could be pre-processed into a series of `if`s without changing its effects. i don't know if that's true in the general case, though. – Woodrow Barlow Jan 04 '18 at 20:49
-
@WoodrowBarlow The compiler definitely wouldn't. I would be surprised if even the JIT compiler could pull it off. But code abstraction and maintainability very often comes at a (hopefully slight) performance cost. Unless you have a demonstrable performance problem, I would say this falls under premature optimization. – shmosel Jan 04 '18 at 20:53
-
-
Could you please guide me here: https://stackoverflow.com/questions/61032842/java-8-to-remove-string-value-from-each-field-where-string-value-comes-of-the ? – PAA Apr 04 '20 at 18:29
Maybe check Hibernate Validator 4.0, the Reference Implementation of the JSR 303: Bean Validation.
This is an example of an annotated class:
public class Address {
@NotNull
private String line1;
private String line2;
private String zip;
private String state;
@Length(max = 20)
@NotNull
private String country;
@Range(min = -2, max = 50, message = "Floor out of range")
public int floor;
...
}
For an introduction, see Getting started with JSR 303 (Bean Validation) – part 1 and part 2 or the "Getting started" section of the reference guide which is part of the Hibernate Validator distribution.

- 562,542
- 136
- 1,062
- 1,124
You can create a function that returns a boolean value and checks every attribute. You can call that function to do the job for you.
Alternatively, you can initialize the object with default values. That way there is no need for you to do any checking.

- 3,011
- 6
- 29
- 36
I don't have enough context to give you a correct answer, but I'll suggest you to make you code immutable as much as possible. Use public final
fields. No more getters
or setters
: every field has to be defined by the constructor
. Your code is shorter, more readable and prevents you from writing code with side effects.
It doesn't prevent you from passing null arguments to your constructor though... You can still check every argument as suggested by @cletus, but I'll suggest you to throw IllegalArgumentException
instead of NullPointerException
that doesn't give no new hint about what you've done.
Anyway, that's what I do as much as I can and it improved my code (readability, stability) to a great extend. Everyone in my team does so and we are very happy with that. We learned that when we try to write some erlang
code where everything is immutable.
Hope this helps.

- 2,617
- 4
- 25
- 47
I tried this and it works without any issues to validate if the field is empty. I have answered your question partially as I haven't personally tried to add default values to attributes
if(field.getText()!= null && !field.getText().isEmpty())
Hope it helps

- 9,833
- 3
- 32
- 58

- 1
This is not to check for null, instead this will be helpful in converting an existing object to an empty object(fresh object). I dont know whether this is relevant or not, but I had such a requirement.
@SuppressWarnings({ "unchecked" })
static void emptyObject(Object obj)
{
Class c1 = obj.getClass();
Field[] fields = c1.getDeclaredFields();
for(Field field : fields)
{
try
{
if(field.getType().getCanonicalName() == "boolean")
{
field.set(obj, false);
}
else if(field.getType().getCanonicalName() == "char")
{
field.set(obj, '\u0000');
}
else if((field.getType().isPrimitive()))
{
field.set(obj, 0);
}
else
{
field.set(obj, null);
}
}
catch(Exception ex)
{
}
}
}