Object getting null in the middle of the process

Asked

Viewed 97 times

3

I am developing a system using Java, Maven, Hibernate, Primefaces and Mysql database. Within this system, I have created a program to record the amount of rainfall that occurs on the day.

My table Pluviometro has the fields for the (auto increment code, location, date and amount of rain).

When I initialize the screen, I have one @PostContruct that initializes my object Pluviometro. After initialization, fill in the 3 fields of the table and click on write.

By clicking the "save" button it calls the method to save the data but the object Pluviometro enough null. Can someone give me a hint of what might be going on?

@PostConstruct
private void startDeTela(){
    PluviometroDAO pluviometroDAO = new PluviometroDAO();
    pluviometros = pluviometroDAO.listar("dataDeApontamento");
    pluviometro = new Pluviometro();
    carregaSelectItens();
}

public void novo(){
    pluviometro = new Pluviometro();
}

public void salvar(){
    PluviometroDAO pluviometroDAO = new PluviometroDAO();
    try{
        pluviometroDAO.merge(pluviometro);
        Messages.addGlobalInfo("Apontamento Registrado Com Sucesso.");
        novo();
    }catch(RuntimeException erro){
        erro.printStackTrace();
    }
}
  • 1

    Hello! It’s not clear where it comes from null. The save button calls which method? Furthermore, I recommend avoiding this type of annotation PostConstruct, are rarely needed and in simpler problems ends up more disturbing than helping.

  • Hello Dherik, it reaches null in the save method().

  • How are you instantiating this class? If you make only one new Classe(), this annotation @PostConstruct (spring, right? ) inside it won’t work.

  • You’re using Spring on this project too?

  • Stateless I’m not using Spring.

  • What I find strange is that on my screen is loading the data of the object Pluviometro. I believe this getting right on screen, otherwise weight would give nullPoint error.

  • This annotation PostConstruct where’s?

  • She is from javax.annotation.Postconstruct;

  • To get an idea, what I did to test. No objeto Pluviometro tenho os campos (Fazenda, Data e chuva). I created the independent fields, installed them and on my screen instead of pointing directly to the Pluviometro, I pointed to these fields. At the time of saving I gave a new rainfall meter and I set these fields. Is giving the impression that the Object Rainfall Meter is having scope problem.

  • I tried to send the code of how it got, but it was disfigured.

Show 5 more comments

1 answer

1

  1. Make life easier for those who are trying to help you and put the full source code, along with xhtml.
  2. In the save method you are merging the rainfall object. This object must be declared in the class scope along with gets and sets. And in xhtml you point each field to the attributes of that object.
  3. If you have CDI as a dependency on the project, use it to inject the Rainfall object with the @Inject annotation. Notice that in the presented code there are 2 lines exactly equal to instantiate it.
  4. If you don’t use CDI in the project, create a variable with the global scope and start it only once via @Postconstruct. This way, you avoid code replication.
  5. In the bean Managed is not interesting to inject a DAO, taking into consideration "design standards". The ideal would be to have the following structure (Managed Bean > Controller > DAO)

If with these instructions you cannot solve the problem in question. Take a look at this link with a functional design.

Browser other questions tagged

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