0

i am try to run a sample application of hibernate, it give me a error on run time:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.NullPointerException at transaction.rollback();

this is in Main.java:

public class Main {

public static void main(String[] args) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        Address address = new Address("ABC", "Delhi", "TN", "110001");
        Student student = new Student("kumar", address);
        session.save(student);
        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
2
  • Do you know which line it's failing on? Commented Aug 16, 2010 at 12:28
  • You can see from the exception output that it was thrown from the main method on a call to transaction.rollback() Commented Aug 16, 2010 at 12:56

1 Answer 1

1

Should change

transaction.rollback();

to

if (transaction != null) {
    transaction.rollback();
}

as its possible for the assignment of transaction to throw an exception.

If you want to get rid of the Log4J messages, you can add a call to

BasicConfigurator.configure();

to setup basic logging

Sign up to request clarification or add additional context in comments.

2 Comments

where i should write this line: BasicConfigurator.configure();
Make it the first line in the main method, you can read more about log4j here - logging.apache.org/log4j/1.2/index.html but note, its been "superceeded" by logback logback.qos.ch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.