473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 25927
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

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

Similar topics

4
by: gabryh | last post by:
Hi, The following code throws me NullPointerException. ..... public static boolean isEmpty(String value) { return ((value == null) || (value.trim().equals(""))); }
3
by: Alan Krueger | last post by:
Greetings, I've been able to cache Transformer objects in a Tomcat-based servlet application to avoid unnecessary Transformer rebuilding, except for certain ones on certain machines. I'm...
1
by: Lars Stenberg | last post by:
Hello! Im sitting here and trying to retreive som avi information, but the invoke of the AVIFileInfo throws a nullpointer exception. snipp ------- public struct AVIFILEINFO { public long...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
1
by: sokeefe | last post by:
I am trying to edit the GUI of a project in Netbeans. In particular, I am trying to add new JButtons. I get a NullPointerException when I try to add an Event to any given JButton (even ones that...
5
oll3i
by: oll3i | last post by:
Map<String,String> rowsMap = null; .... for(int i=0; i<numberOfRows; i++) rowsMap= new HashMap<String, String>(); throws nullpointerexception ? thank You
1
by: r035198x | last post by:
This exception occurs often enough in practice to warrant its own article. It is a very silly exception to get because it's one of the easiest exceptions to avoid in programming. Yet we've all got it...
4
by: phpuser123 | last post by:
That's what I've implemented... import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.