1
You guys are my last salvation. I get this mistake javax.faces.el.EvaluationException: java.lang.NullPointerException
when I try to register a client. My experience with development is low, I am developing this small system as a college job.
Stack Trace:
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.getStatus(JtaStatusHelper.java:76)
at org.hibernate.engine.transaction.internal.jta.JtaStatusHelper.isActive(JtaStatusHelper.java:118)
at org.hibernate.engine.transaction.internal.jta.CMTTransaction.join(CMTTransaction.java:149)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.joinTransaction(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.postInit(AbstractEntityManagerImpl.java:210)
at org.hibernate.jpa.internal.EntityManagerImpl.<init>(EntityManagerImpl.java:91)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.internalCreateEntityManager(EntityManagerFactoryImpl.java:345)
at org.hibernate.jpa.internal.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:313)
at bean.JPAUtil.getEntityManager(JPAUtil.java:22)
at bean.ClienteBean.salva(ClienteBean.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.el.parser.AstValue.invoke(AstValue.java:275)
at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
bean class:
package bean;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Cliente;
@ManagedBean
@SessionScoped
public class ClienteBean {
private Cliente cliente;
private List<Cliente> clientes;
public ClienteBean() {
cliente = new Cliente();
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public List<Cliente> getClientes() {
if (this.clientes == null) {
EntityManager em = JPAUtil.getEntityManager();
Query a = em.createQuery("select a from Cliente a",
Cliente.class);
this.clientes = a.getResultList();
em.close();
}
return clientes;
}
/**
*
* @return
*/
public String salva() {
EntityManager em = JPAUtil.getEntityManager();
try {
em.getTransaction().begin();
// Verifica se o cliente ainda não está salva no banco de dados.
if (cliente.getId() == null) {
//Salva os dados do cliente.
em.persist(cliente);
} else {
//Atualiza os dados da pessoa.
cliente = em.merge(cliente);
}
// Finaliza a transação.
em.getTransaction().commit();
} finally {
em.close();
}
this.clientes = null;
return "index";
}
public void excluir(Cliente cliente) {
EntityManager em = JPAUtil.getEntityManager();
em.getTransaction().begin();
cliente = em.merge(cliente);
em.remove(cliente);
em.getTransaction().commit();
em.close();
this.clientes = null;
}
public String alterar(Cliente a) {
this.cliente = a;
return "cadastro";
}
public String cadastrarNovo() {
cliente = new Cliente();
return "cadastro";
}
}
Jpautil:
package bean;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtil {
private static final EntityManagerFactory
emf = Persistence.createEntityManagerFactory("EcommercePulsePU");
public static EntityManager getEntityManager() {
return emf.createEntityManager();
}
public static void fechaEntityManager() {
emf.close();
}
}
persistence.xml, configured to create tables (not yet created)
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="EcommercePulsePU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/ecommercePG</jta-data-source>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/postgres"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="1234"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
Registration page Primefaces JSF:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<link rel="stylesheet" href="Resources/css/ecommerceCSS.css"/>
<link rel="stylesheet" href="Resources/css/bootstrap-theme.css"/>
<link rel="stylesheet" href="Resources/css/bootstrap-theme.min.css"/>
<link rel="stylesheet" href="Resources/css/bootstrap.css"/>
<link rel="stylesheet" href="Resources/css/bootstrap.min.css"/>
<link rel="stylesheet" href="Resources/css/main.css"/>
<title>Química Pulse</title>
</h:head>
<h:body styleClass="body">
<p:layout fullPage="true">
<div id="page">
<div id="header">
<ui:insert name="header" >
<ui:include src="/template/menusuperior.xhtml" />
</ui:insert>
</div>
</div>
<p:layoutUnit position="center" size="300" >
<div style="background-color: #1E86A6;">
<h:form style="color: white; border: #1E86A6; border-width: 20pt; border-style: solid;">
<div class="row">
<div class="col-xs-4 col-md-4">
<p:outputLabel for="nome" value="Nome:" />
<h:inputText styleClass="col-md-3 form-control" id="nome" value="#{clienteBean.cliente.nome}" title="Nome" required="true"/>
</div>
<div class="col-xs-4 col-md-2">
<p:outputLabel for="cpf" value="CPF:" />
<h:inputText styleClass="col-md-3 form-control" id="cpf" value="#{clienteBean.cliente.cpf}" title="CPF" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<p:outputLabel for="email" value="Email:" />
<h:inputText styleClass="col-md-3 form-control" id="email" value="#{clienteBean.cliente.email}" title="E-mail" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<p:outputLabel for="rua" value="Rua:" />
<h:inputText styleClass="col-md-3 form-control" id="rua" value="#{clienteBean.cliente.rua}" title="Rua" required="true"/>
</div>
<div class="col-xs-4 col-md-2">
<p:outputLabel for="numero" value="Nº:" />
<h:inputText styleClass="col-md-3 form-control" id="numero" value="#{clienteBean.cliente.numero}" title="Numero" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<p:outputLabel for="cidade" value="Cidade:" />
<h:inputText styleClass="col-md-3 form-control" id="cidade" value="#{clienteBean.cliente.cidade}" title="Cidade" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<p:outputLabel for="pais" value="País:" />
<h:inputText styleClass="col-md-3 form-control" id="pais" value="#{clienteBean.cliente.pais}" title="Pais" required="true"/>
</div>
<div class="col-xs-4 col-md-4">
<p:outputLabel for="cep" value="CEP:" />
<h:inputText styleClass="col-md-3 form-control" id="cep" value="#{clienteBean.cliente.cep}" title="CEP" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<h:outputLabel value="Telefone: " for="telefone" />
<h:inputText class="col-md-3 form-control" id="telefone" value="#{clienteBean.cliente.telefone}" title="Telefone" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<h:outputLabel value="Usuário: " for="usuario" />
<h:inputText class="col-md-3 form-control" id="usuario" value="#{clienteBean.cliente.usuario}" title="Usuario" required="true"/>
</div>
</div>
<div class="row">
<div class="col-xs-4 col-md-4">
<h:outputLabel value="Senha: " for="senha" />
<p:password styleClass="col-md-3 form-control" id="senha" value="#{clienteBean.cliente.senha}" feedback="true" required="true"/>
</div></div>
<div class="row">
<div class="col-xs-4 col-md-4">
<h:outputLabel value="Confirmar Senha: " for="confirmasenha" />
<p:password styleClass="col-md-3 form-control" id="confirmasenha" value="#{clienteBean.cliente.confirmasenha}" title="Confirmar Senha" required="true"/>
</div>
</div>
<br/>
<div>
<h:commandButton styleClass="btn btn-success" value="Cadastrar" action="#{clienteBean.salva}"/>
</div>
</h:form></div>
</p:layoutUnit>
</p:layout>
</h:body>
</html>
Project libraries:
From now on, I’d like to thank anyone who can help me. Thank you!
What is the version of Java? What packages are you putting in your classpath and/or modulepath?
– Victor Stafusa
Post the page code. Another thing: in two methods you do
this.cliente = null;
. Don’t do it. It gives you a very easy way to receive aNullPointerException
. For example: If you invoke the methodsalva()
twice in a row, the second time you will receive an NPE on the lineif (cliente.getId() == null)
.– igventurelli
How do you recommend I place? Thanks for the reply
– William Vitor