1
In the project I use hibernate
, and when I try to save a new client, it just shows the class validation message CadastroClienteService
. The strange thing is that all fields are filled in the form, it seems null pointer
, but I did not identify where is presenting the error. If someone identify, I thank in advance the strength.
-> These are the project classes:
Client class
package com.tacocell.controleservicos.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(name = "clientes")
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
private Long codigo;
private String nomeCliente;
private String cnpj;
private String cpf;
private String endereco;
private String bairro;
private int cep;
private String telefoneFixo;
private String telefoneCel;
private String email;
private Date dataCadastro;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getCodigo() {
return codigo;
}
public void setCodigo(Long codigo) {
this.codigo = codigo;
}
@Column(name="nm_cli")
public String getNomeCliente() {
return nomeCliente;
}
public void setNomeCliente(String ncliente) {
this.nomeCliente = ncliente;
}
public String getCnpj() {
return cnpj;
}
public void setCnpj(String cnpj) {
this.cnpj = cnpj;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
this.bairro = bairro;
}
public int getCep() {
return cep;
}
public void setCep(int cep) {
this.cep = cep;
}
@Column(name="telfixo")
public String getTelefoneFixo() {
return telefoneFixo;
}
public void setTelefoneFixo(String telefoneFixo) {
this.telefoneFixo = telefoneFixo;
}
@Column(name="telcel")
public String getTelefoneCel() {
return telefoneCel;
}
public void setTelefoneCel(String telefoneCel) {
this.telefoneCel = telefoneCel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Temporal(TemporalType.DATE)
@Column(name="dt_cad")
public Date getDataCadastro() {
return dataCadastro;
}
public void setDataCadastro(Date dataCadastro) {
this.dataCadastro = dataCadastro;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((codigo == null) ? 0 : codigo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Cliente other = (Cliente) obj;
if (codigo == null) {
if (other.codigo != null)
return false;
} else if (!codigo.equals(other.codigo))
return false;
return true;
}
}
Class Customer registration
package com.tacocell.controleservicos.controller;
import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
import com.tacocell.controleservicos.util.jsf.FacesUtil;
import com.tacocell.controleservicos.model.Cliente;
import com.tacocell.controleservicos.service.CadastroClienteService;
import com.tacocell.controleservicos.service.NegocioException;
@Named
@ViewScoped
public class CadastroClienteBean implements Serializable {
private static final long serialVersionUID = 1L;
private Cliente cliente;
@Inject
private CadastroClienteService cadastroClienteService;
public CadastroClienteBean(){
limpar();
}
public void salvar() {
try{
this.cadastroClienteService.salvar(cliente);
FacesUtil.addSuccessMessage("Cliente salvo com sucesso!");
}catch(NegocioException ne) {
FacesUtil.addErrorMessage(ne.getMessage());
}
limpar();
}
public void limpar() {
this.cliente = new Cliente();
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
}
Classe Clientedao
package com.tacocell.controleservicos.dao;
import java.io.Serializable;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import com.tacocell.controleservicos.model.Cliente;
public class ClienteDAO implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private EntityManager manager;
public void salvar(Cliente cliente) {
manager.merge(cliente);
}
@SuppressWarnings("unchecked")
public List<Cliente> filtrar(Cliente cliente){
Session session = manager.unwrap(Session.class);
Criteria criteria = session.createCriteria(Cliente.class);
if (cliente.getNomeCliente() != null && !cliente.getNomeCliente().trim().equals("")) {
criteria.add(Restrictions.ilike("nm_cli", cliente.getNomeCliente(), MatchMode.ANYWHERE));
}
if (cliente.getCnpj() != null && !cliente.getCnpj().trim().equals("")) {
criteria.add(Restrictions.ilike("cnpj", cliente.getCnpj(), MatchMode.ANYWHERE));
}
return criteria.list();
}
public Cliente buscarPeloCodigo(Long codigo) {
return manager.find(Cliente.class, codigo);
}
}
Class Customer registration
package com.tacocell.controleservicos.service;
import java.io.Serializable;
import java.util.Date;
import javax.inject.Inject;
import com.tacocell.controleservicos.dao.ClienteDAO;
import com.tacocell.controleservicos.model.Cliente;
import com.tacocell.controleservicos.util.jpa.Transactional;
public class CadastroClienteService implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
private ClienteDAO clienteDAO;
@Transactional
public void salvar(Cliente cliente) throws NegocioException {
if (cliente.getNomeCliente() == null || cliente.getNomeCliente().trim().equals("")) {
throw new NegocioException("Este campo é obrigatório. Preencha o nome do cliente");
}
/*
if (cliente.getCnpj() == null || cliente.getCnpj().trim().equals("")) {
throw new NegocioException("O cnpj do cliente e obrigatorio");
}
*/
if (cliente.getCodigo() == null)
{
cliente.setDataCadastro(new Date());
}
this.clienteDAO.salvar(cliente);
}
}
Can add the error stack where you saw the "null Pointer" ?
– user28595
Because then, this code is based on the course I’m doing, and I’ve made some adaptations to this example, and I no longer have course support for doubts. I consulted a friend and he said it could be this mistake, "NULL POINTER".
– Diego.Santos
No error message appears in the log
– Diego.Santos
So how do you say in the question: *"(...)it seems null Pointer(...)""? Where did you see this null Pointer? See on the console if an error pops up when you run. Paste here the error that appears.
– user28595
In the Customer Registration class in the save method, the error message that is generated when the exception is captured. is this error that appears.Regarding Pointer null, in another forum I had this suggestion that could be this.
– Diego.Santos