-->
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.  [ 4 posts ] 
Author Message
 Post subject: Null Pointer.. please help.. :)
PostPosted: Thu Oct 01, 2009 12:32 pm 
Newbie

Joined: Sat Sep 19, 2009 12:25 am
Posts: 7
Hi all,

I am having problem with "Null Pointer" issue.
It happened at line transaction = session.beginTransaction(); in CommonUtility.java

Below is the source code:
CommonUtility.java
Code:
package com.univ.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CommonUtility {

public void create(Object obj){
   Session session = HibernateUtil.getSessionFactory().openSession();
   Transaction transaction = null;
   
   try{
      System.out.println("CommonUtility - create is running");
      transaction = session.beginTransaction();
      
      session.save(obj);
      transaction.commit();
      System.out.println("CommonUtility - transaction is commited successfully");
   
  }catch (Exception e) {
    transaction.rollback();
    e.printStackTrace();   
}finally {
     session.close();
    }
   }
}


HibernateUtil.java
Code:
package com.univ.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   static {
      try {
         sessionFactory = new Configuration().configure().buildSessionFactory();
      }catch (Throwable ex) {
         System.err.println("Initial SessionFactory creation failed." + ex);
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static SessionFactory getSessionFactory() {
      return sessionFactory;
   }
}


Top
 Profile  
 
 Post subject: Re: Null Pointer.. please help.. :)
PostPosted: Thu Oct 01, 2009 1:19 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi suigion,

could you please post the full stack trace of the NPE? Then probably someone can help.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Null Pointer.. please help.. :)
PostPosted: Fri Oct 02, 2009 9:56 am 
Newbie

Joined: Sat Sep 19, 2009 12:25 am
Posts: 7
here is the detail of the error:

Code:
Exception in thread "main" java.lang.NullPointerException
   at com.univ.util.CommonUtility.create(CommonUtility.java:22)
   at com.univ.process.CourseMethods.createCourse(CourseMethods.java:18)
   at com.univ.process.Tester.main(Tester.java:36)


CourseMethods.java
Code:
package com.univ.process;

import com.univ.util.CommonUtility;
import com.univ.pojo.Course;

public class CourseMethods {

   private Course course;

   
   public void setCourse(Course course) {
      this.course = course;
   }
   
   public void createCourse(){
      System.out.println("createCourse is running");
      CommonUtility cu = new CommonUtility();
      cu.create(course);
      System.out.println("createCourse is completed successfully");
   }
}


Tester.java
Code:
package com.univ.process;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import com.univ.pojo.Course;
import com.univ.process.CourseMethods;

public class Tester {

   
   
   public static void main(String[] args){
      Date dte=null;
      float courseFee=12000;
      
      Course c = new Course();
      CourseMethods cm = new CourseMethods();
      DateFormat df = new SimpleDateFormat("dd/mm/yyyy");
      
      try{
         dte = df.parse("22/12/2009");
      }catch(Exception e){
         e.getMessage();
      }
      
      c.setCourseName("Diploma in Computing");
      c.setCourseDesc("Diploma in Computing Description");
      c.setCourseEffectiveDate(dte);
      c.setCourseFee(courseFee);
      c.setCourseUniversity("Curtin University Of Technology");
      c.setCourseIsDeleted('0');
      
      cm.setCourse(c);
   
      cm.createCourse();
      
   }
}


Top
 Profile  
 
 Post subject: Re: Null Pointer.. please help.. :)
PostPosted: Fri Oct 02, 2009 10:17 am 
Newbie

Joined: Sat Sep 19, 2009 12:25 am
Posts: 7
Hi,

Issue solved. I was actually pointing to the wrong database in my hibernate config. file.. :)

thanks..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.