By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,654 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,654 IT Pros & Developers. It's quick & easy.

Exception in thread "main" java.lang.NullPointerException

P: 1
Please help me, I am new in hiernate.

I am using hibernate 3 and postgresql database.

I am using the below classes and xml:

Expand|Select|Wrap|Line Numbers
  1. package org.javabrains.koushik.dto;
  2.  
  3. import javax.persistence.Entity;
  4. import javax.persistence.Id;
  5.  
  6. @Entity
  7. public class UserDetails {
  8.     @Id
  9. private int userId;
  10. private String userName;
  11. public int getUserId() {
  12.     return userId;
  13. }
  14. public void setUserId(int userId) {
  15.     this.userId = userId;
  16. }
  17. public String getUserName() {
  18.     return userName;
  19. }
  20. public void setUserName(String userName) {
  21.     this.userName = userName;
  22. }
  23.  
  24. }
  25.  
************************************
Expand|Select|Wrap|Line Numbers
  1.  package org.koushik.hibernate;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. import org.javabrains.koushik.dto.UserDetails;
  7.  
  8. public class HibernateTest {
  9.  
  10.     /**
  11.      * @param args
  12.      */
  13.     static Session session;
  14.     public static void main(String[] args) {
  15.         UserDetails user=new UserDetails();
  16.         user.setUserId(1);
  17.         user.setUserName("First User");
  18.         try
  19.         {
  20.         SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
  21.         session=sessionFactory.openSession();
  22.         session.beginTransaction();
  23.         session.save(user);
  24.         }
  25.         catch(Exception e)
  26.         {
  27.         session.getTransaction().commit();
  28.         }
  29.     }
  30.  
  31. }
********************************************
Expand|Select|Wrap|Line Numbers
  1.   <?xml version="1.0" encoding="utf-8" ?> 
  2. - <!--   ~ Hibernate, Relational Persistence for Idiomatic Java
  3.   ~
  4.   ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
  5.   ~ indicated by the @author tags or express copyright attribution
  6.   ~ statements applied by the authors.  All third-party contributions are
  7.   ~ distributed under license by Red Hat Inc.
  8.   ~
  9.   ~ This copyrighted material is made available to anyone wishing to use, modify,
  10.   ~ copy, or redistribute it subject to the terms and conditions of the GNU
  11.   ~ Lesser General Public License, as published by the Free Software Foundation.
  12.   ~
  13.   ~ This program is distributed in the hope that it will be useful,
  14.   ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15.   ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  16.   ~ for more details.
  17.   ~
  18.   ~ You should have received a copy of the GNU Lesser General Public License
  19.   ~ along with this distribution; if not, write to:
  20.   ~ Free Software Foundation, Inc.
  21.   ~ 51 Franklin Street, Fifth Floor
  22.   ~ Boston, MA  02110-1301  USA
  23.  
  24.   --> 
  25.  <!DOCTYPE hibernate-configuration PUBLIC
  26. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  27. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  28. - <hibernate-configuration>
  29. - <session-factory>
  30. - <!--  Database connection settings 
  31.   --> 
  32.   <property name="connection.driver_class">org.postgresql.Driver</property> 
  33.   <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property> 
  34.   <property name="connection.username">postgres</property> 
  35.   <property name="connection.password">password</property> 
  36. - <!--  JDBC connection pool (use the built-in) 
  37.   --> 
  38.   <property name="connection.pool_size">1</property> 
  39. - <!--  SQL dialect 
  40.   --> 
  41.   <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
  42. - <!--  Disable the second-level cache  
  43.   --> 
  44.   <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
  45. - <!--  Echo all executed SQL to stdout 
  46.   --> 
  47.   <property name="show_sql">true</property> 
  48. - <!--  Drop and re-create the database schema on startup 
  49.   --> 
  50.   <property name="hbm2ddl.auto">create</property> 
  51. - <!--  Names the annotated entity class 
  52.   --> 
  53.   <mapping class="org.javabrains.koushik.dto.UserDetails" /> 
  54.   </session-factory>
  55.   </hibernate-configuration>
Jan 24 '13 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.