Boa tarde pessoal.
Estou seguindo essa video aula http://www.youtube.com/watch?v=VvdRKdDY7VM e cheguei até a parte 5 acompanhei todos os testes e todos ficaram certos, no entanto, na hora do submit da erro NullPointerException bem no final da aula 5 (esse erro não aparece na aula mas revisei todo o projeto, imports e tudo está idêntico).
Não estou conseguindo identificar a origem do erro debuguei o projeto na classe Contato (getNome) ele retorna o nome que digitei no input como deveria ser mas depois disso não consigo mais debugar.
<?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">
<h:head>
<title>Sistema Agenda</title>
</h:head>
<h:body>
<center><h1>Sistema Agenda</h1></center><br/>
<h:form>
<h:outputText value="Nome"/>
<h:inputText id="nome" value="#{contatosController.contatos.nome}"/>
<h:outputText value="E-mail"/>
<h:inputText id="email" value="#{contatosController.contatos.email}"/>
<h:outputText value="Telefone"/>
<h:inputText id="telefone" value="#{contatosController.contatos.telefone}"/>
<h:outputText value="Celular"/>
<h:inputText id="celular" value="#{contatosController.contatos.celular}"/>
<h:commandButton value="Salvar" id="salva" actionListener="#{contatosController.adicionar(actionEvent)}"/>
</h:form>
</h:body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/agenda</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="br.com.agenda.model.Contatos"/>
</session-factory>
</hibernate-configuration>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.agenda.util;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
/**
* Hibernate Utility class with a convenient method to get Session Factory
* object.
*
* @author 18122012
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
</web-app>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.agenda.dao;
import br.com.agenda.model.Contatos;
import java.util.List;
/**
*
* @author 18122012
*/
public interface InterfaceContatos {
public Contatos getContatos(Long id);
public void salvar(Contatos contatos);
public void remover(Contatos contatos);
public void atualizar(Contatos contatos);
public List<Contatos> list();
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.agenda.model;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
*
* @author 18122012
*/
@Entity(name = "contatos")
public class Contatos implements Serializable {
@Id
@GeneratedValue
private Long id;
private String nome;
private String email;
private String telefone;
private String celular;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getCelular() {
return celular;
}
public void setCelular(String celular) {
this.celular = celular;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.agenda.dao;
import br.com.agenda.model.Contatos;
import br.com.agenda.util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
/**
*
* @author 18122012
*/
public class ContatosDao implements InterfaceContatos {
@Override
public Contatos getContatos(Long id) {
Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
return (Contatos) ss.load(Contatos.class, id);
}
@Override
public void salvar(Contatos contatos) {
Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
ss.beginTransaction();
ss.save(contatos);
ss.beginTransaction().commit();
}
@Override
public void remover(Contatos contatos) {
Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
ss.beginTransaction();
ss.delete(contatos);
ss.beginTransaction().commit();
}
@Override
public void atualizar(Contatos contatos) {
Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
ss.beginTransaction();
ss.update(contatos);
ss.beginTransaction().commit();
}
@Override
public List<Contatos> list() {
Session ss = HibernateUtil.getSessionFactory().getCurrentSession();
ss.beginTransaction();
List lista = ss.createQuery("from contatos").list();
ss.beginTransaction().commit();
return lista;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package br.com.agenda.controller;
import br.com.agenda.dao.ContatosDao;
import br.com.agenda.dao.InterfaceContatos;
import br.com.agenda.model.Contatos;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
/**
*
* @author 18122012
*/
@ManagedBean
@SessionScoped
public class ContatosController {
private Contatos contatos;
private DataModel listaContatos;
public Contatos getContatos() {
if (this.contatos == null) {
this.contatos = new Contatos();
}
return contatos;
}
public void setContatos(Contatos contatos) {
this.contatos = contatos;
}
public DataModel getListaContatos() {
List<Contatos> lista = new ContatosDao().list();
listaContatos = new ListDataModel(lista);
return listaContatos;
}
public void prepararAdicionarContato(ActionEvent actionEvent) {
contatos = new Contatos();
}
public void prepararAlterarContato(ActionEvent actionEvent) {
contatos = (Contatos) (listaContatos.getRowData());
}
public void adicionar(ActionEvent actionEvent) {
InterfaceContatos dao = new ContatosDao();
dao.salvar(contatos);
}
public void alterar(ActionEvent actionEvent) {
InterfaceContatos dao = new ContatosDao();
dao.atualizar(contatos);
}
public String excluir() {
Contatos contato = (Contatos) (listaContatos.getRowData());
InterfaceContatos dao = new ContatosDao();
dao.remover(contato);
return "index";
}
}
Essa descrição aparece no navegador StackTrace
java.lang.NullPointerException
at java.lang.Class.isAssignableFrom(Native Method)
at org.apache.el.util.ReflectionUtil.isAssignableFrom(ReflectionUtil.java:319)
at org.apache.el.util.ReflectionUtil.getMethod(ReflectionUtil.java:185)
at org.apache.el.parser.AstValue.invoke(AstValue.java:271)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:148 )
at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88 )
at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:769)
at javax.faces.component.UICommand.broadcast(UICommand.java:300)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
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:118 )
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
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:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118 )
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
Não sei se isso ajuda ComponentTree também do navegador
<UIViewRoot id="j_id1" inView="true" locale="pt_BR" renderKitId="HTML_BASIC" rendered="true" transient="false" viewId="/index.xhtml">
<?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">
<UIOutput id="j_idt4" inView="true" rendered="true" transient="false">
<title>Sistema Agenda</title>
</UIOutput>
<UIOutput id="j_idt6" inView="true" rendered="true" transient="false">
<center><h1>Sistema Agenda</h1></center>
<HtmlForm enctype="application/x-www-form-urlencoded" id="j_idt8" inView="true" prependId="true" rendered="true" submitted="true" transient="false">
<HtmlOutputText escape="true" id="j_idt9" inView="true" rendered="true" transient="false" value="Nome"/>
<HtmlInputText disabled="false" id="nome" immediate="false" inView="true" localValueSet="false" maxlength="-[telefone removido]" readonly="false" rendered="true" required="false" size="-[telefone removido]" transient="false" valid="true" value="1"/>
<HtmlOutputText escape="true" id="j_idt10" inView="true" rendered="true" transient="false" value="E-mail"/>
<HtmlInputText disabled="false" id="email" immediate="false" inView="true" localValueSet="false" maxlength="-[telefone removido]" readonly="false" rendered="true" required="false" size="-[telefone removido]" transient="false" valid="true" value="1@1"/>
<HtmlOutputText escape="true" id="j_idt11" inView="true" rendered="true" transient="false" value="Telefone"/>
<HtmlInputText disabled="false" id="telefone" immediate="false" inView="true" localValueSet="false" maxlength="-[telefone removido]" readonly="false" rendered="true" required="false" size="-[telefone removido]" transient="false" valid="true" value="1"/>
<HtmlOutputText escape="true" id="j_idt12" inView="true" rendered="true" transient="false" value="Celular"/>
<HtmlInputText disabled="false" id="celular" immediate="false" inView="true" localValueSet="false" maxlength="-[telefone removido]" readonly="false" rendered="true" required="false" size="-[telefone removido]" transient="false" valid="true" value="11"/>
<HtmlCommandButton disabled="false" id="salva" immediate="false" inView="true" readonly="false" rendered="true" transient="false" type="submit" value="Salvar"/>
</HtmlForm>
</UIOutput>
</html>
</UIViewRoot>