javax.faces.FacesException, java.lang.NullPointerException

0

The following error (full description below):

  

com.sun.faces.lifecycle.InvokeApplicationPhase execute WARNING: # {testHibernate.save}: java.lang.NullPointerException javax.faces.FacesException: # {testHibernate.save}

This happens when I click the register button or it will not let me insert it into the database.

Classes

UserAction.java

package DAO;

import Classhiber.Utilizador;
import java.util.List;

public interface UtilizadorDAO {
    public void addutilizador (Utilizador ut);
    public List<Utilizador> findallutilizador();

}

UserDaoimpl.java

package Impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import Classhiber.Utilizador;
import DAO.UtilizadorDAO;

public class UtilizadorDAOimpl implements UtilizadorDAO {
    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public void addutilizador(Utilizador ut) {
        Session session = this.sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.persist(ut);
        tx.commit();
        session.close();    }

    public List<Utilizador> findallutilizador() {
        Session session = this.sessionFactory.openSession();
        List<Utilizador> Utilizadorlist = session.createQuery("from Utilizador").list();
        session.close();        
        return Utilizadorlist;
    }


}

testHibernate.java

 import java.util.List;

    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    import Classhiber.Utilizador;
    import DAO.UtilizadorDAO;
    import Impl.UtilizadorDAOimpl;
    @ManagedBean
    @SessionScoped
    public class TesteHibernate {

        public UtilizadorDAO utdao;

        private Utilizador utiliza;

        public TesteHibernate()
        {
             utdao = new UtilizadorDAOimpl();
             utiliza = new Utilizador();    
        }

        public UtilizadorDAO getUtilizadorDAO() {
            return utdao;
        }

        public void setUtiliza(UtilizadorDAOimpl ut) {
            this.utdao = ut;
        }

        public Utilizador getUtiliza() {
            return utiliza;
        }

        public void setUtiliza(Utilizador utz) {
            this.utiliza = utz;
        }

        public void save() {
            // Calling Business Service
            utiliza.setIdUt(12);
            System.out.println(utiliza.getUser());
            System.out.println(utiliza.getPass());
            System.out.println(utiliza.getTip());

            utdao.addutilizador(utiliza);
        }
        }

Spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3307/xp" />
        <property name="username" value="root" />
        <property name="password" value="" />
    </bean>


<!-- Hibernate 4 SessionFactory Bean definition -->
<bean id="hibernate4AnnotatedSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>Classhiber.Utilizador</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.show_sql">false</prop>
            </props>
        </property>
    </bean>


    <bean id="UtilizadorDAOimpl" class="Impl.UtilizadorDAOimpl">
        <property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory" />
    </bean>
</beans>

index.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <script name="jquery/jquery.js" library="primefaces"></script>
    <title>Register Utilizador</title>
</h:head>
<h:form>
    <p:growl id="messages"></p:growl>
    <p:panelGrid columns="2">
        <p:outputLabel value="Enter Utilizdor User:"></p:outputLabel>
        <p:inputText value="#{testeHibernate.utiliza.user}"></p:inputText>
        <p:outputLabel value="Enter Utilizador Pass:"></p:outputLabel>
        <p:inputText value="#{testeHibernate.utiliza.pass}"></p:inputText>
        <p:outputLabel value="Enter Utilizador tip"></p:outputLabel>
        <p:inputText value="#{testeHibernate.utiliza.tip}"></p:inputText>                     
    </p:panelGrid>
    <p:commandButton value="Register" action="#{testeHibernate.save}" ></p:commandButton>
</h:form>
</html>

Error

 com.sun.faces.lifecycle.InvokeApplicationPhase execute
WARNING: #{testeHibernate.save}: java.lang.NullPointerException
javax.faces.FacesException: #{testeHibernate.save}: java.lang.NullPointerException
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)
    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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: 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)
    ... 26 more
Caused by: java.lang.NullPointerException
    at Impl.UtilizadorDAOimpl.addutilizador(UtilizadorDAOimpl.java:29)
    at TesteHibernate.save(TesteHibernate.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 27 more

1
3
Ago 29, 2015 8:07:19 PM com.sun.faces.context.AjaxExceptionHandlerImpl handlePartialResponseError
SEVERE: 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.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at Impl.UtilizadorDAOimpl.addutilizador(UtilizadorDAOimpl.java:29)
    at TesteHibernate.save(TesteHibernate.java:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 27 more

Thank you. (Sorry if you have any serious programming errors but I'm learning)

    
asked by anonymous 28.08.2015 / 16:01

2 answers

1

In your class testeHibernate.java :

public UtilizadorDAO utdao;
It is not instantiated (so it is null) and is being used in save() method:
So the error:

  

{testHibernate.save}: java.lang.NullPointerException

public void save() {
    // Calling Business Service
    utiliza.setIdUt(12);
    System.out.println(utiliza.getUser());
    System.out.println(utiliza.getPass());
    System.out.println(utiliza.getTip());
    utdao.addutilizador(utiliza);
}

There are two ways to correct this, one of them is to instantiate utdao by declaring it (just like utiliza )

public class testeHibernate {
    public UtilizadorDAO utdao = new UtilizadorDAO();
    private Utilizador utiliza = new Utilizador();
    ...
}

Or create a constructor to instantiate all the variables that will be used (I recommend using this).

A constructor must be public , with no return type and must have the same class name. A constructor for your class would look something like:

public TesteHibernate(){
  utdao = new UtilizadorDAO();
  utiliza = new Utilizador();
}

There is still another way, which is to instantiate in the% method itself with%

public class testeHibernate {
    public UtilizadorDAO utdao = new UtilizadorDAO();
    private Utilizador utiliza = new Utilizador();
    ...
}

But this is not recommended, after all you would have to instantiate the variable in all methods that were used there.

    
28.08.2015 / 16:15
1
 #{testeHibernate.save}: java.lang.NullPointerException

Within the save method you are using the variables utiliza and utDAO

You initialized the variable uitiliza but did not initialize the utDAO .

My suggestion is to create a constructor for the class TesteHibernate to initialize the variables you need.

OBS: On the other topic I suggested putting the first letter of the uppercase class. I see it's still tiny.

Builder code:

public TesteHibernate(){
  utdao = new UtilizadorDAO();
  utiliza = new Utilizador();
}

The name of the constructor must be the same as the name of the class. I put TesteHibernate uppercase assuming you will change the name of the class by putting TesteHibernate uppercase too.

When put inside the utiliza = new Utilizador(); constructor you can change where the variable declaration is and leave only Utilizador utiliza;

    
28.08.2015 / 16:14