空指针
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import cn.jbit.houserent.entity.District;
import cn.jbit.houserent.entity.Street;
public class test2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
District district = new District();
Street street1 = new Street();
Street street2 = new Street();
Street street3 = new Street();
district.setName("丰台");
street1.setName("广安路");
street1.setDistrict(district);
street2.setName("大红门路");
street2.setDistrict(district);
street3.setName("南苑路");
street3.setDistrict(district);
SessionFactory sessionFactory = null;
Session session = null;
Transaction tx= null;
try{
sessionFactory =
new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
tx= session.beginTransaction();
session.save(street1);
session.save(street2);
session.save(street3);
session.save(district);
tx.commit();
}catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally{
session.close();
sessionFactory.close();
}
}
}
控制台提示
Exception in thread "main" java.lang.NullPointerException
at test2.main(test2.java:83)
[最优解释]
test2.java:83
83行指的上面的哪一行。。。
[其他解释]
楼主代码没贴全
怀疑是以下方法有问题
sessionFactory = new Configuration().configure()
.buildSessionFactory();
session = sessionFactory.openSession();
sessionFactory =
new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();