javax.faces.Facesexception, java.lang.Nullpointerexception

Asked

Viewed 3,180 times

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 press the register button or let me enter in the database.

Classes

Usersjava.

package DAO;

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

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

}

Usersoimpl.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. (I’m sorry if there’s some serious programming error but I’m learning)

  • I’ve put that part of the code in

2 answers

1

 #{testeHibernate.save}: java.lang.NullPointerException

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

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

My suggestion is to create a builder for the class TesteHibernate for initialize the variables that needs.

OBS: In the other topic I suggested to put the first letter of the uppercase class. I see that it is still lowercase.

Code of the builder:

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

The constructor name needs to be the same class name. I put TesteHibernate uppercase that you will change the class name by placing the TesteHibernate capital also.

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

1


In your class testeHibernate.java:

public UtilizadorDAO utdao;
It is not instantiated (therefore null) and is being used in the method save():
That’s why the mistake:

{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 fix this, one of them is to instantiate utdao when declaring it (as you did with utiliza)

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

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

A builder must be public, without any kind of return 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 save()

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

but that would not be recommended, after all you would have to instantiate the variable in all the methods it was using-there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.