How to check if there is a null attribute in the object?

Asked

Viewed 2,244 times

2

I have a class Person and I want to check if there is any null attribute, any one because I can’t save a person null-attribute. I want to avoid a lot of if. Is there any way to do that?

public void salvar(Pessoa p) throws Exception {

    if (p.getNome() == null) {
        throw new Exception("Nome nulo");
    } else if (p.getIdade == null) {
        throw new Exception("Idade nula");
    } else if (p.getCorDosOlhos == null) {
        throw new Exception("CorDosOlhos nulo");
    }
}
  • 3

    The best way is for the values of these attributes to be passed in the constructor. This ensures that they have been initialized in each of the instantiated objects. At the same time the class should only provide getters for those attributes so as to keep yours as valid.

  • I don’t have much knowledge about Java, but tried to search if there is any 'Required' in the attributes? In the case of C# you have 'Required' which makes the field mandatory. But you could try to do what @ramaral suggested, the problem would be that the attribute would go with a default value.

  • 1

    @Leonardocoelho For this case there would be no "standard value": the name of the person does not change, the color of the eyes also not, in the case of age the correct is to request the date of birth and this also does not change.

  • Of course this does not prevent the null test on the builder, but I think that is where it should be done.

  • 1

    @I’m sorry, I got your answer wrong at first, but now I think I understand your answer.

1 answer

4


As I said in my comments, if you need all the data to have some value, the ideal is for the guy to guarantee it for himself. Can be via constructor and methods Setter (validating or being absent) in addition to automatic initialization of members.

If this is not possible, if it depends on context, that is, time can have null members, time can not, ever thought that has two problems and perhaps mere two different types? One allows the null and the other does not allow. So at some point you can use one to create the other, and obviously what does not allow nulls will fail if it is not properly initialized.

Doesn’t that solve? Okay, you’ve found the most obvious solution. And if you want to make specific exceptions for each member that fails, there are no simple solutions.

If you can have a general solution for any failing member, you can use reflection. With this technique you can sweep all members in a generic way and check the values. It is not very performative but it solves in most cases. If you have different cases (exceptional) you will have to treat all this.

If you need performance you can use a code generator to analyze the class and create a code that checks all members one by one directly without writing the code for each class. Obviously you will need to run a tool to generate new code every time someone moves the class (this can be automated too, obvious).

It is still possible to automate the process a little, even though having different treatments for each member. This will require a framework or a more complex code generation tool. Either way members will need to annotations or some other way to inform what to do with each of them.

Everything will depend on convention to automate better.

With such a general question it is difficult to give a more specific solution.

Example of reflection:

for (Field field : Pessoa.class.getDeclaredFields()) {
    if (Modifier.isPrivate(field.getModifiers())) {
        //faz alguma coisa aqui com field
    }
}

I put in the Github for future reference.

Documentation.

Tutorial.

Question in the OS on the subject. Other.

I’m not a fan of the term attribute, I prefer field.

  • I thank everyone, they were answers that helped me to better formulate my code. I’m going to choose what @ramaral said about filling the attributes in the constructor and providing only the getters

Browser other questions tagged

You are not signed in. Login or sign up in order to post.