gpt4 book ai didi

java - Hibernate Java 中 session 的空指针异常

转载 作者:行者123 更新时间:2023-12-01 10:17:18 25 4
gpt4 key购买 nike

我正在创建一个简单的应用程序,由于某种原因,我不断收到空指针异常。我的猜测是,这与我的 session.open 位于 if 语句内有关?我收到的错误出现在finally block 上,其中显示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();
}

}

}
}

错误:

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)

最佳答案

该行上的 NullPointerException 在堆栈跟踪中没有其他内容只能意味着 session 为空。据我所知,唯一可能发生的情况是,如果 session = sessionFactory.openSession(); 赋值从未执行 - 这意味着在 中发生异常sessionFactory.openSession()HibernateUtil.getSessionFactory() 中,该异常被 finally block 中引发的后续异常隐藏。

我希望,如果您在调试器中单步执行此代码,您将看到执行流程从这两行之一跳转到 catch block ,然后遇到 NullPointerException on transaction.rollback() (因为事务分配也从未发生过)并跳转到 finally block ,在那里它遇到另一个 NullPointerException 并且摆脱困境。

关于java - Hibernate Java 中 session 的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35821692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com