473,545 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hibernate throws nullpointerexce ption

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

when i flush the session hibernate throws nullpointerexce ption

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 25952
oll3i
679 Contributor
my hibernate.cfg.x ml 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 Contributor
i changed my hibernate.cfg.x ml 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 Recognized Expert MVP
Isn't there a stacktrace and line number available somewhere?

kind regards,

Jos
Oct 17 '08 #4
oll3i
679 Contributor
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/DocumentExcepti on
Exception in thread "main" java.lang.NullP ointerException
at FirstExample.ma in(FirstExample .java:30)
Oct 17 '08 #5
JosAH
11,448 Recognized Expert MVP
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/DocumentExcepti on
Exception in thread "main" java.lang.NullP ointerException
at FirstExample.ma in(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 Contributor
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/DocumentExcepti on
Exception in thread "main" java.lang.NullP ointerException
at project1.FirstE xample.main(Fir stExample.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.x ml file
seems like a sessionfactory is not created at all
Oct 17 '08 #7
JosAH
11,448 Recognized Expert MVP
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 Contributor
there You go :)


Error in creating SessionFactory object.org/dom4j/DocumentExcepti on
Exception in thread "main" java.lang.Excep tionInInitializ erError
at project1.FirstE xample.main(Fir stExample.java: 31)
Caused by: java.lang.NoCla ssDefFoundError : org/dom4j/DocumentExcepti on
at project1.FirstE xample.main(Fir stExample.java: 15)
Caused by: java.lang.Class NotFoundExcepti on: org.dom4j.Docum entException
at java.net.URLCla ssLoader$1.run( URLClassLoader. java:200)
at java.security.A ccessController .doPrivileged(N ative Method)
at java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:188)
at java.lang.Class Loader.loadClas s(ClassLoader.j ava:307)
at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:301 )
at java.lang.Class Loader.loadClas s(ClassLoader.j ava:252)
at java.lang.Class Loader.loadClas sInternal(Class Loader.java:320 )
... 1 more
Oct 17 '08 #9
oll3i
679 Contributor
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.c fg.Environment <clinit>
INFO: Hibernate 3.2.5
2008-10-17 21:22:18 org.hibernate.c fg.Environment <clinit>
INFO: hibernate.prope rties not found
2008-10-17 21:22:19 org.hibernate.c fg.Environment buildBytecodePr ovider
INFO: Bytecode provider name : cglib
2008-10-17 21:22:19 org.hibernate.c fg.Environment <clinit>
INFO: using JDK 1.4 java.sql.Timest amp handling
2008-10-17 21:22:19 org.hibernate.c fg.Configuratio n configure
INFO: configuring from resource: /hibernate.cfg.x ml
2008-10-17 21:22:19 org.hibernate.c fg.Configuratio n getConfiguratio nInputStream
INFO: Configuration resource: /hibernate.cfg.x ml
Error in creating SessionFactory object./hibernate.cfg.x ml not found
Exception in thread "main" java.lang.Excep tionInInitializ erError
at project1.FirstE xample.main(Fir stExample.java: 31)
Caused by: org.hibernate.H ibernateExcepti on: /hibernate.cfg.x ml not found
at org.hibernate.u til.ConfigHelpe r.getResourceAs Stream(ConfigHe lper.java:147)
at org.hibernate.c fg.Configuratio n.getConfigurat ionInputStream( Configuration.j ava:1405)
at org.hibernate.c fg.Configuratio n.configure(Con figuration.java :1427)
at org.hibernate.c fg.Configuratio n.configure(Con figuration.java :1414)
at project1.FirstE xample.main(Fir stExample.java: 15)
Oct 17 '08 #10

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

Similar topics

4
4062
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
3158
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 running Tomcat 4.1.27 under Eclipse 2.1.0 using the Sysdeo Tomcat plugin using j2re1.4.1_02 under Windows 2000 SP4. I've digested this down to a small...
1
4174
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 dwMaxBytesPerSec; public long dwFlags;
1
2406
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 TextField t1, t2, t3;
2
2264
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to...
1
3184
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 already exist) in my application using the Design Editor. I go about this by right-clicking on the JButton in the design editor, navigating to Events...
5
1790
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
17528
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 before, lending proof to Einstein's statement: “Only two things are infinite, the universe and human stupidity ...”. Main Cause Dereferencing...
4
1906
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 javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; public class java_xml extends DefaultHandler{
0
7405
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7659
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7760
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5975
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4949
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3455
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1887
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.