[英]Null Pointer Exception on Session in Hibernate Java
I am creating a simple application and for some reason I keep getting null pointer exception. 我正在创建一个简单的应用程序,由于某种原因,我不断收到空指针异常。 My guess is that it has something to do with my session.open being inside a if statement? 我的猜测是,这与我的session.open是否与if语句有关? The error I am getting is on the finally block where it says session.close(); 我得到的错误是在finally块上,它说session.close();。
import java.util.*;
import javax.persistence.*;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class OrderTester {
private static SessionFactory sessionFactory;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Session session = null;
Transaction transaction = null;
System.out.println("Press 1 to log in or press 2 to sign up");
int n = scan.nextInt();
if (n == 1) {
Customer c = new Customer();
// c.logIn();
} else if (n == 2) {
try {
sessionFactory = HibernateUtil.getSessionFactory();
session = sessionFactory.openSession();
transaction = session.beginTransaction();
Customer c = new Customer();
Address a = new Address();
session.save(c);
session.save(a);
Scanner read = new Scanner(System.in);
System.out.println("Enter a username");
String userName = read.next();
c.setUsername(userName);
System.out.println("Enter a password");
String password = read.next();
c.setPassword(password);
System.out.println("Enter the street name");
String streetName = read.next();
a.setStreetName(streetName);
System.out.println("Enter city");
String city = read.next();
a.setCity(city);
System.out.println("Enter state");
String state = read.next();
a.setState(state);
System.out.println("Enter zipcode");
String zipcode = read.next();
a.setZipCode(zipcode);
read.close();
transaction.commit();
} catch (Exception ex) {
transaction.rollback();
System.out.println("Transaction is rolled back.");
} finally {
session.close();
sessionFactory.close();
}
}
}
}
The error: 错误:
Press 1 to log in or press 2 to sign up
2
Mar 05, 2016 4:51:44 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
Mar 05, 2016 4:51:44 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.11.Final}
Mar 05, 2016 4:51:44 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 05, 2016 4:51:44 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 05, 2016 4:51:44 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Mar 05, 2016 4:51:44 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Mar 05, 2016 4:51:45 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Exception in thread "main" java.lang.NullPointerException
at Pizza.OrderTester.main(OrderTester.java:65)
A NullPointerException
on that line with nothing else in the stack trace can only mean that session
is null. 该行上的NullPointerException
堆栈跟踪中没有其他内容仅表示session
为null。 The only way that could happen, as far as I can tell, is if the session = sessionFactory.openSession();
据我所知,可能发生的唯一方法是session = sessionFactory.openSession();
assignment is never executed - which would mean that an exception is happening either in sessionFactory.openSession()
or in HibernateUtil.getSessionFactory()
, with that exception being hidden by the later exception raised in the finally
block. 分配从未执行-这意味着在sessionFactory.openSession()
或HibernateUtil.getSessionFactory()
中发生异常,该异常被finally
块中引发的更高版本的异常隐藏。
I expect that if you step through this code in a debugger you will see the execution flow jump from one of those two lines to the catch
block, then hit a NullPointerException
on transaction.rollback()
(because the transaction assignment never happened either) and jump to the finally
block, where it hits another NullPointerException
and bails out. 我希望,如果您在调试器中单步执行此代码,您将看到执行流从这两行之一跳到catch
块,然后在transaction.rollback()
上命中NullPointerException
(因为事务分配从未发生过)和跳转到finally
块,在此撞上另一个NullPointerException
并退出。
Since both session
and sessionFactory
are initialized as null
outside the try/catch block, never assume they will not be null in finally
clause and call close()
directly. 由于在try / catch块之外将session
和sessionFactory
都初始化为null
,所以永远不要假设在finally
子句中它们将不为null并直接调用close()
。 Change it to the following: 将其更改为以下内容:
} catch (Exception ex) {
if(transaction != null) {
try {
transaction.rollback();
} catch (Exception e) { // Log error }
}
ex.printStackTrace();
System.out.println("Transaction is rolled back.");
} finally {
if(session != null) {
try {
session.close();
} catch (Exception e) { // Log error }
}
if(sessionFactory != null) {
try {
sessionFactory.close();
} catch (Exception e) { // Log error }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.