Object getting null in the middle of the process

3

I'm developing a system using Java, Maven, Hibernate, PrimeFaces and MySQL database. Within this system, I created a program to record the amount of rainfall that occurs on the day.

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

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

Clicking the "save" button calls the method to save the data, but the Pluviometro object arrives null . Can anyone 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();
    }
}
    
asked by anonymous 25.05.2018 / 02:57

1 answer

1
  • Facilitate the life of those who are trying to help you and put the complete source code together with xhtml.
  • In the save method you are doing the merge of the rain gauge object. This object must be declared in the scope of the class along with the gets and sets. And in xhtml you point each field to the attributes of that object.
  • If you have the CDI as a dependency in the project, use it to inject the PluviometroDAO object with the @Inject annotation. Note that in the code presented there are 2 lines exactly the same to instantiate it.
  • If you do not use the CDI in the project, create a variable with the global scope and initialize it only once through the @PostConstruct. That way, you avoid code replication.
  • No managed bean is not interesting to inject a DAO, taking into account "design patterns". The ideal would be to have the following structure (Managed Bean & Controller & DAO)
  • If you can not solve the problem with these instructions. Take a look at this link with a functional design.

        
    25.05.2018 / 22:45