Boa tarde, o meu projeto tava tudo rodando e to fazendo um curso de CDI e aqui ja deu problema kk :( segue o meu código.
Meu bean com CDI
import java.io.Serializable;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import br.com.bercalini.dao.ClienteDao;
import br.com.bercalini.dao.EnderecoDao;
import br.com.bercalini.modelo.Cliente;
import br.com.bercalini.modelo.Endereco;
import br.com.bercalini.util.MensagemUtil;
import br.com.bercalini.util.Redirecionador;
@Named
@javax.enterprise.context.SessionScoped
public class ClienteBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private Cliente cliente = new Cliente();
private Endereco endereco = new Endereco();
@Inject
private ClienteDao clienteDao;
@Inject
private EnderecoDao enderecoDao;
private List<Cliente> clientes = clienteDao.listaTodos();
public void adicionar() {
if (cliente.getCodigo() == null) {
cliente.setEndereco(endereco);
enderecoDao.adicionar(endereco);
clienteDao.adicionar(cliente);
new MensagemUtil().mensagemInfo("CLIENTE adicionado com SUCESSO");
this.clientes = new ClienteDao().listaTodos();
} else {
clienteDao.alterar(cliente);
enderecoDao.alterar(endereco);
new MensagemUtil().mensagemInfo("CLIENTE atualizada com SUCESSO");
}
this.cliente = new Cliente();
this.endereco = new Endereco();
}
public void deletar(Cliente cliente) {
new MensagemUtil().mensagemInfo("CIENTE removido com SUCESSO " + cliente.getNome());
clienteDao.deletar(cliente);
enderecoDao.deletar(cliente.getEndereco());
this.cliente = new Cliente();
}
public Redirecionador alterar(Cliente cliente) {
this.cliente = cliente;
this.endereco = cliente.getEndereco();
return new Redirecionador("cliente");
}
public List<Cliente> getListaClientes() {
return clienteDao.listaTodos();
}
public Redirecionador formularioCadastro() {
cliente = new Cliente();
endereco = new Endereco();
return new Redirecionador("cliente");
}
public Redirecionador formularioClientes() {
return new Redirecionador("clientePesquisa");
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public Endereco getEndereco() {
return endereco;
}
public void setEndereco(Endereco endereco) {
this.endereco = endereco;
}
public List<Cliente> getClientes() {
if (clientes == null) {
clienteDao.listaTodos();
}
return clientes;
}
public void setClientes(List<Cliente> clientes) {
this.clientes = clientes;
}
}
Meu DAO com CDI.
import java.io.Serializable;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import br.com.bercalini.modelo.Cliente;
public class ClienteDao implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Inject
private EntityManager em;
public ClienteDao() {}
public ClienteDao(EntityManager em) {
this.em = em;
}
public void adicionar(Cliente cliente) {
em.getTransaction().begin();
em.persist(cliente);
em.getTransaction().commit();
}
@SuppressWarnings("unchecked")
public List<Cliente> listaTodos() {
Query query = em.createNamedQuery("Cliente.listaClientes");
List<Cliente> lista = query.getResultList();
return lista;
}
public void deletar(Cliente cliente) {
em.getTransaction().begin();
em.remove(em.merge(cliente));
em.getTransaction().commit();
}
public void alterar(Cliente cliente) {
em.getTransaction().begin();
em.merge(cliente);
em.getTransaction().commit();
}
public Cliente buscaPorId(Long codigo) {
Cliente cliente = em.find(Cliente.class, codigo);
return cliente;
}
}
public class EnderecoDao implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private EntityManager em;
public void adicionar(Endereco endereco) {
em.getTransaction().begin();
em.persist(endereco);
em.getTransaction().commit();
}
public void alterar(Endereco endereco) {
em.getTransaction().begin();
em.merge(endereco);
em.getTransaction().commit();
}
public void deletar(Endereco endereco) {
em.getTransaction().begin();
em.remove(em.merge(endereco));
em.getTransaction().commit();
}
}
jan 06, 2017 2:35:34 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/tcc] threw exception [null] with root cause
java.lang.NullPointerException
at br.com.bercalini.bean.ClienteBean.<init>(ClienteBean.java:31)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:119)
at org.jboss.weld.injection.ConstructorInjectionPoint.invokeAroundConstructCallbacks(ConstructorInjectionPoint.java:92)
at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:78)
at org.jboss.weld.injection.producer.AbstractInstantiator.newInstance(AbstractInstantiator.java:28)
at org.jboss.weld.injection.producer.BasicInjectionTarget.produce(BasicInjectionTarget.java:116)
at org.jboss.weld.injection.producer.BeanInjectionTarget.produce(BeanInjectionTarget.java:180)
at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:158)
at org.jboss.weld.util.bean.IsolatedForwardingBean.create(IsolatedForwardingBean.java:45)
at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:96)
at org.jboss.weld.context.PassivatingContextWrapper$AbstractPassivatingContextWrapper.get(PassivatingContextWrapper.java:76)
at org.jboss.weld.bean.ContextualInstanceStrategy$DefaultContextualInstanceStrategy.get(ContextualInstanceStrategy.java:101)
at org.jboss.weld.bean.ContextualInstanceStrategy$CachingContextualInstanceStrategy.get(ContextualInstanceStrategy.java:178)
at org.jboss.weld.bean.ContextualInstance.get(ContextualInstance.java:50)
at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:742)
at org.jboss.weld.el.AbstractWeldELResolver.lookup(AbstractWeldELResolver.java:107)
at org.jboss.weld.el.AbstractWeldELResolver.getValue(AbstractWeldELResolver.java:90)
at org.jboss.weld.environment.servlet.util.ForwardingELResolver.getValue(ForwardingELResolver.java:49)
at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:67)
at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:72)
at org.apache.el.parser.AstValue.getValue(AstValue.java:161)
at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185)
at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:109)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at javax.faces.component.UIInput.getValue(UIInput.java:291)
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:94)
at org.primefaces.util.ComponentUtils.getValueToRender(ComponentUtils.java:58)
at org.primefaces.component.inputtext.InputTextRenderer.encodeMarkup(InputTextRenderer.java:71)
at org.primefaces.component.inputtext.InputTextRenderer.encodeEnd(InputTextRenderer.java:52)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:72)
at org.primefaces.component.fieldset.FieldsetRenderer.encodeContent(FieldsetRenderer.java:94)
at org.primefaces.component.fieldset.FieldsetRenderer.encodeMarkup(FieldsetRenderer.java:76)
at org.primefaces.component.fieldset.FieldsetRenderer.encodeEnd(FieldsetRenderer.java:47)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:890)
at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:85)
at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:72)
at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:920)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:458)
at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:134)
at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
O erro vem quando eu puxo uma lista que me retorna nulla nessa parte, mais como disse estava tudo rodando certo, acho que é alguma anotação ou um erro que não to prestando atenção.