-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Error java.lang.NullPointerException hibernate
PostPosted: Thu Mar 06, 2008 6:50 am 
Newbie

Joined: Thu Mar 06, 2008 5:08 am
Posts: 3
hello everybody;

I'm developping a web application using Struts 1.1 , Hibernate 3.1 and Mysql , and I've got this message error:

Quote:
Etat HTTP 500 -

--------------------------------------------------------------------------------

type Rapport d'exception

message

description Le serveur a rencontré une erreur interne () qui l'a empêché de satisfaire la requête.

exception

javax.servlet.ServletException
org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:545)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:486)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


cause mère

java.lang.NullPointerException
com.yourcompany.struts.action.CandidatInscriptionAction.execute(CandidatInscriptionAction.java:98)
org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note La trace complète de la cause mère de cette erreur est disponible dans les fichiers journaux de Apache Tomcat/5.5.17.



Here is the code of my action :
Code:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.action;
import com.myeclipse.hibernate.*;
import java.util.*;
 
 
import org.hibernate.Transaction;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.*;
import com.yourcompany.struts.form.CandidatInscriptionForm;
 
/**
* MyEclipse Struts
* Creation date: 02-25-2008
*
* XDoclet definition:
* @struts.action path="/candidatInscription" name="candidatInscriptionForm" input="/form/candidatInscription.jsp" scope="request" validate="true"
*/
public class CandidatInscriptionAction extends Action {
   /*
    * Generated Methods
    */
 
   /**
    * Method execute
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return ActionForward
    */
   public ActionForward execute(ActionMapping mapping, ActionForm form,
         HttpServletRequest request, HttpServletResponse response) {
       CandidatInscriptionForm candidatInscriptionForm = (CandidatInscriptionForm) form;// TODO Auto-generated method stub
       
      // Recuperation des donnée du formulaire
       
       String courriel=candidatInscriptionForm.getTXT_Courriel();
       String password=candidatInscriptionForm.getTXT_MotDePasse();
       int civilite=candidatInscriptionForm.getDDL_Civilite();
       String nom=candidatInscriptionForm.getTXT_Nom();
       String prenom=candidatInscriptionForm.getTXT_Prenom();
       int jour=candidatInscriptionForm.getTXT_JourNaissance();
       int mois=candidatInscriptionForm.getDDL_MoisNaissance();
       int annee=candidatInscriptionForm.getDDL_AnneeNaissance();
       String adresse=candidatInscriptionForm.getTXT_Adresse();
       String code_postal=candidatInscriptionForm.getTXT_CodePostal();
       String ville=candidatInscriptionForm.getTXT_Ville();
       Integer pays=candidatInscriptionForm.getDDL_Pays();
       String fixe=candidatInscriptionForm.getTXT_TelephoneFixe();
       String mobile=candidatInscriptionForm.getTXT_TelephonePortable();
       int situation=candidatInscriptionForm.getDDL_SituationProfessionnelle();
       int type=candidatInscriptionForm.getCBL_TypeContact();
       Date date= new Date(annee,mois,jour);
       
       
       
       
       // initialisation de l'utilisateur
       
          Date d = new Date();
         oxy_utilisateur utilisateur = new oxy_utilisateur();
         utilisateur.setIdUtilisateur(1);
         utilisateur.setEmailUtilisateur(courriel);
         utilisateur.setPasswordUtilisateur(password);
         utilisateur.setNomUtilisateur(nom);
         utilisateur.setPrenomUtilisateur(prenom);
         utilisateur.setCreationUtilisateur(d);
         
         
         oxy_candidat candidat = new oxy_candidat();
         candidat.setIdCandidat(1);
         candidat.setIdUtilisateur(utilisateur.getIdUtilisateur());
         candidat.setIdCivilite(civilite);
         candidat.setDateNaissanceCandidat(date);
         candidat.setAdresseCandidat(adresse);
         candidat.setCodePostalCandidat(code_postal);
         candidat.setVilleCandidat(ville);
         candidat.setIdPays(pays);
         candidat.setTelephoneFixeCandidat(fixe);
         candidat.setTelephoneMobileCandidat(mobile);
         candidat.setIdTypeContact(type);
         
         
         // 2. Create DAO
         oxy_candidatDAO dao = new oxy_candidatDAO();
         oxy_utilisateurDAO dao1= new oxy_utilisateurDAO();
         // 3. Start the transaction
         Transaction tx = dao.getSession().beginTransaction();
         Transaction tx1= dao1.getSession().beginTransaction();
         // 4. Add user
         dao.save(candidat);
         dao1.save(utilisateur);
         
         // 5. Commit the transaction (write to database)
         tx.commit();
         tx1.commit();
         // 6. Close the session (cleanup connections)
         dao.getSession().close();
          dao1.getSession().close();
       
       
       
       
       
       
       
       
       
       
       
       
         
         
         
         
         
         
         
         
         
         
         
         
         
         
         
      return mapping.findForward("success");
       
   }
   
   
   // Ajout Candidat
 
}



I think that this error eccured from the line :

Transaction tx = dao.getSession().beginTransaction();

Please help me !!!!


Top
 Profile  
 
 Post subject: It seems session is null
PostPosted: Thu Mar 06, 2008 8:50 am 
Newbie

Joined: Mon Sep 03, 2007 9:10 am
Posts: 7
It seems the session in your dao is null.

Consider delegate transaction management, using Spring for example. Otherwise, catch exceptions and commit/rollback as needed.

I use this config with Spring, accessing the data with hibernateTemplate bean:

Code:
...

   <bean id="sessionFactory"
      class="es.sadiel.sirgo.hibernate.LocalSessionFactoryBean">
      <property name="configLocation">
         <value>classpath:hibernate.cfg.xml</value>
      </property>
      <property name="configurationClass">
         <value>org.hibernate.cfg.AnnotationConfiguration</value>
      </property>
   </bean>
   <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>
   
   <bean id="hibernateTemplate"
      class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>

   <!--
      This bean is a post-processor that will automatically apply relevant advisors
      to any bean in child factories.
   -->
   <bean
      class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

   <!--
      AOP advisor that will provide declarative transaction management based on
      attributes. It's possible to add arbitrary custom Advisor implementations
      as well, and they will also be evaluated and applied automatically.
   -->
   <bean
      class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
      <property name="transactionInterceptor">
         <ref bean="transactionInterceptor" />
      </property>
   </bean>

   <!--
      Transaction interceptor to use for auto-proxy creation.
      Transaction attributes will be read in from JDK 1.5+ annotations.
   -->
   <bean id="transactionInterceptor"
      class="org.springframework.transaction.interceptor.TransactionInterceptor">
      <property name="transactionManager">
         <ref bean="transactionManager" />
      </property>
      <property name="transactionAttributeSource">
         <bean
            class="org.springframework.transaction.annotation.AnnotationTransactionAttributeSource" />
      </property>
   </bean>

   <!-- Bean definition is needed to rollback transactions after a test. -->
   <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"
      destroy-method="close">
      <property name="driverClassName"
         value="oracle.jdbc.driver.OracleDriver" />
      <property name="url"
         value="jdbc:oracle:thin:@localhost:1521:sid" />
      <property name="username" value="user" />
      <property name="password" value="password" />
   </bean>


Top
 Profile  
 
 Post subject: NullPointerException on insert
PostPosted: Mon Sep 15, 2008 6:51 pm 
Beginner
Beginner

Joined: Sun Oct 14, 2007 7:29 pm
Posts: 23
I too am getting a NullPointerException when I call the session.save(object) method. I have the log4j logger set to debug, and I am not getting any useful info out of Hibernate, although the insert is actually occuring in the database.

Using Hibernate 3.2.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.