469,328 Members | 1,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

hibernate throws nullpointerexception

oll3i
679 512MB
Thank you for the last post :) now another problem

when i flush the session hibernate throws nullpointerexception

my code looks as follows

Expand|Select|Wrap|Line Numbers
  1. import org.hibernate.Session;
  2. import org.hibernate.SessionFactory;
  3. import org.hibernate.cfg.Configuration;
  4.  
  5. public class FirstExample {
  6.   public static void main(String[] args) {
  7.     Session session = null;
  8.  
  9.     try{
  10.       // W tym kroku odczytamy plik hibernate.cfg.xml i przygotujemy Hibernate do użycia     
  11.         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  12.         session = sessionFactory.openSession();
  13.  
  14.         //Stworzenie nowego obiektu Contact i ustawienie jego atrybutów
  15.          System.out.println("Inserting Record");
  16.         Contact contact = new Contact();
  17.         contact.setId(3);
  18.         contact.setFirstName("Jan");
  19.         contact.setLastName("Kowalski");
  20.         contact.setEmail("jan.kowalski@email.com");
  21.         //zapisanie zmian dokonanych od ostatniego zapisu
  22.         session.save(contact);
  23.         System.out.println("Done");
  24.     }catch(Exception e){
  25.       System.out.println(e.getMessage());
  26.     }finally{
  27.       // wymuszenie wysłania "oczekujących" operacji do bazy danych
  28.       session.flush();
  29.       // zamknięcie sesji
  30.       session.close();
  31.       }
  32.   }
  33. }
  34.  
Oct 17 '08 #1
15 25302
oll3i
679 512MB
my hibernate.cfg.xml file


Expand|Select|Wrap|Line Numbers
  1. <?xml version='1.0' encoding='utf-8'?>
  2.         <!DOCTYPE hibernate-configuration PUBLIC
  3.         "-//Hibeornate/Hibernate Configuration DTD//EN"
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
  5.         <hibernate-configuration>
  6.     <session-factory>
  7.               <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
  8.               <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:1701</property>
  9.               <property name="hibernate.connection.username">sa</property>
  10.               <property name="hibernate.connection.password"></property>
  11.               <property name="hibernate.connection.pool_size">1</property>
  12.               <property name="show_sql">true</property>
  13.               <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
  14.               <property name="hibernate.hbm2ddl.auto">update</property>
  15.               <!-- Mapping files -->
  16.               <mapping resource="Contact.hbm.xml"/>
  17.     </session-factory>
  18.         </hibernate-configuration>
  19.  
Oct 17 '08 #2
oll3i
679 512MB
i changed my hibernate.cfg.xml to

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE hibernate-configuration PUBLIC
  2. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  3. "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  4. <hibernate-configuration>
  5.     <session-factory>
  6.               <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>
  7.               <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost:1701</property>
  8.               <property name="hibernate.connection.username">sa</property>
  9.               <property name="hibernate.connection.password"></property>
  10.               <property name="hibernate.connection.pool_size">1</property>
  11.               <property name="show_sql">true</property>
  12.               <property name="dialect">org.hibernate.dialect.HSQLDialect</property>
  13.               <property name="hibernate.hbm2ddl.auto">update</property>
  14.               <!-- Mapping files -->
  15.               <mapping resource="Contact.hbm.xml"/>
  16.     </session-factory>
  17.         </hibernate-configuration>
  18.  
Oct 17 '08 #3
JosAH
11,448 Expert 8TB
Isn't there a stacktrace and line number available somewhere?

kind regards,

Jos
Oct 17 '08 #4
oll3i
679 512MB
i added to my code

Expand|Select|Wrap|Line Numbers
  1.  } catch (Throwable e) {
  2.         System.err.println("Error in creating SessionFactory object." 
  3.             + e.getMessage());
  4.         throw new ExceptionInInitializerError(e);
  5.     }
  6.  
and now i get

Error in creating SessionFactory object.org/dom4j/DocumentException
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:30)
Oct 17 '08 #5
JosAH
11,448 Expert 8TB
i added to my code

Expand|Select|Wrap|Line Numbers
  1.  } catch (Throwable e) {
  2.         System.err.println("Error in creating SessionFactory object." 
  3.             + e.getMessage());
  4.         throw new ExceptionInInitializerError(e);
  5.     }
  6.  
and now i get

Error in creating SessionFactory object.org/dom4j/DocumentException
Exception in thread "main" java.lang.NullPointerException
at FirstExample.main(FirstExample.java:30)
According to your code in your OP that should be 'session.close()' which I don't
believe. Please include the line(s) indicated by the line number in the exception.
You can't leave us guessing like that.

kind regards,

Jos
Oct 17 '08 #6
oll3i
679 512MB
sorry

the code is

Expand|Select|Wrap|Line Numbers
  1. package project1;
  2.  
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6.  
  7.  
  8.  
  9. public class FirstExample {
  10.   public static void main(String[] args) {
  11.     Session session = null;
  12.  
  13.     try{
  14.       // W tym kroku odczytamy plik hibernate.cfg.xml i przygotujemy Hibernate do użycia     
  15.         SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  16.         session = sessionFactory.openSession();
  17.  
  18.         //Stworzenie nowego obiektu Contact i ustawienie jego atrybutów
  19.          System.out.println("Inserting Record");
  20.         Contact contact = new Contact();
  21.         contact.setId(3);
  22.         contact.setFirstName("Jan");
  23.         contact.setLastName("Kowalski");
  24.         contact.setEmail("jan.kowalski@email.com");
  25.         //zapisanie zmian dokonanych od ostatniego zapisu
  26.         session.save(contact);
  27.         System.out.println("Done");
  28.     } catch (Throwable e) {
  29.         System.err.println("Error in creating SessionFactory object." 
  30.             + e.getMessage());
  31.         throw new ExceptionInInitializerError(e);
  32.     }finally{
  33.       // wymuszenie wysłania "oczekujących" operacji do bazy danych
  34.       session.flush();
  35.       // zamknięcie sesji
  36.       session.close();
  37.       }
  38.   }
  39. }
  40.  
and the error is


Error in creating SessionFactory object.org/dom4j/DocumentException
Exception in thread "main" java.lang.NullPointerException
at project1.FirstExample.main(FirstExample.java:34)

so it is session.flush when i comment it, it is session.close that causes the error

maybe there is something wrong with my hibernate.cfg.xml file
seems like a sessionfactory is not created at all
Oct 17 '08 #7
JosAH
11,448 Expert 8TB
Yep, that's it and your finally clause is wrong: if there is no session yet you
can't flush() nor close() it. Make that finally clause:

Expand|Select|Wrap|Line Numbers
  1. if (session != null) {
  2.    session.flush(); 
  3.    session.close(); 
  4. }
  5.  
... and print out the entire exception stacktrace in your catch clause:

Expand|Select|Wrap|Line Numbers
  1. e.printStackTrace();
  2.  
... and see what happened instead of hiding it.

kind regards,

Jos
Oct 17 '08 #8
oll3i
679 512MB
there You go :)


Error in creating SessionFactory object.org/dom4j/DocumentException
Exception in thread "main" java.lang.ExceptionInInitializerError
at project1.FirstExample.main(FirstExample.java:31)
Caused by: java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at project1.FirstExample.main(FirstExample.java:15)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
... 1 more
Oct 17 '08 #9
oll3i
679 512MB
i added more jars and now i get hihi i just can not wait gotta do something

2008-10-17 21:22:18 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
2008-10-17 21:22:18 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
2008-10-17 21:22:19 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
2008-10-17 21:22:19 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
2008-10-17 21:22:19 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
2008-10-17 21:22:19 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
Error in creating SessionFactory object./hibernate.cfg.xml not found
Exception in thread "main" java.lang.ExceptionInInitializerError
at project1.FirstExample.main(FirstExample.java:31)
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStrea m(ConfigHelper.java:147)
at org.hibernate.cfg.Configuration.getConfigurationIn putStream(Configuration.java:1405)
at org.hibernate.cfg.Configuration.configure(Configur ation.java:1427)
at org.hibernate.cfg.Configuration.configure(Configur ation.java:1414)
at project1.FirstExample.main(FirstExample.java:15)
Oct 17 '08 #10
oll3i
679 512MB
and another one now it sees the files

2008-10-17 21:34:40 org.hibernate.cfg.Environment <clinit>
INFO: Hibernate 3.2.5
2008-10-17 21:34:41 org.hibernate.cfg.Environment <clinit>
INFO: hibernate.properties not found
2008-10-17 21:34:41 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: Bytecode provider name : cglib
2008-10-17 21:34:41 org.hibernate.cfg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timestamp handling
2008-10-17 21:34:41 org.hibernate.cfg.Configuration configure
INFO: configuring from resource: /hibernate.cfg.xml
2008-10-17 21:34:41 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: Configuration resource: /hibernate.cfg.xml
2008-10-17 21:34:43 org.hibernate.cfg.Configuration addResource
INFO: Reading mappings from resource : Contact.hbm.xml
Error in creating SessionFactory object.org/apache/commons/collections/SequencedHashMap
Exception in thread "main" java.lang.ExceptionInInitializerError
at project1.FirstExample.main(FirstExample.java:31)
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/collections/SequencedHashMap
at org.hibernate.mapping.Table.<init>(Table.java:33)
at org.hibernate.cfg.Mappings.addTable(Mappings.java: 165)
at org.hibernate.cfg.HbmBinder.bindRootPersistentClas sCommonValues(HbmBinder.java:290)
at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinde r.java:273)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.jav a:144)
at org.hibernate.cfg.Configuration.add(Configuration. java:669)
at org.hibernate.cfg.Configuration.addInputStream(Con figuration.java:504)
at org.hibernate.cfg.Configuration.addResource(Config uration.java:566)
at org.hibernate.cfg.Configuration.parseMappingElemen t(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactor y(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Config uration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Config uration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configur ation.java:1428)
at org.hibernate.cfg.Configuration.configure(Configur ation.java:1414)
at project1.FirstExample.main(FirstExample.java:15)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.collections.SequencedHashMap
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
... 15 more
Oct 17 '08 #11
JosAH
11,448 Expert 8TB
Hold on; no need to make this the longest thread ever; now it can't find the class
SequencedHashMap. You have a classpath problem: include all the jars needed
by your application, if you miss one your exception stacktrace will tell you which
class couldn't be found: include it in your classpath until all is fine.

kind regards,

Jos
Oct 17 '08 #12
oll3i
679 512MB
sure :) i like adding those jars
Oct 17 '08 #13
oll3i
679 512MB
NOW everything almost works except that it doesn't insert the values into the hsql table
Oct 17 '08 #14
usually when u are inserting a new record you don't specify the object id, by specifying the id you are telling hibernate to update an existing object.

I see you have enabled the option to see the SQL hibernate generates, what is it generating, is it and INSERT or UPDATE?
Oct 22 '08 #15
How are you checking if the insert is happening, through sql only or by retrieving the values in some way? If there is insert statement and still not getting the values in table, worth checking the auto commit feature. Something similar to oracle config mentioned here.
May 14 '13 #16

Post your reply

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

Similar topics

4 posts views Thread by gabryh | last post: by
1 post views Thread by Lars Stenberg | last post: by
1 post views Thread by ketand1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.