简体   繁体   中英

getting Exception in thread “main” java.lang.NullPointerException from hibernate

Why i'm getting this error even while i'm running just a hello program(in separate java project). actually this error coming from hibernate, but can any one explain me what is the issue and where it is and how to fix it.

Please find my hibernate code:

public static void main(String[] args) {

    Student_Info student_Info = new Student_Info();

    student_Info.setName("xyz");
    student_Info.setRollNo(101);
    student_Info.setLastName("kumar");
    student_Info.setRecordUpdatedDate(new Date());
    student_Info.setRecordUpdatedTime(new Date());
    student_Info.setRecordUpdatedOn(new Date());
    student_Info.setIsActive(false);
    student_Info.setDescription("hi this is xyz.");

    Session session = HibernateUtil.getCurrentSession();
    Transaction transaction = null;
    try {
        transaction = session.beginTransaction();
        session.save(student_Info);
        transaction.commit();
    }catch (Exception e) {
        if (transaction != null)
            transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();                            //close session
        HibernateUtil.getSessionFactory().close();  //close sessionFactory
    }
}

Please find code of Student_Info

@Entity @Table(name="student")

public class Student_Info {

@Id
@GeneratedValue
private int rollNo;

private String name;

@Transient
@Column(name="lastName", nullable=false)
private String lastName;

@Temporal(TemporalType.DATE)
private Date recordUpdatedDate;

@Temporal(TemporalType.TIME)
private Date recordUpdatedTime;

private Date recordUpdatedOn;

private Boolean isActive;

@Lob
private String description;

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public Boolean getIsActive() {
    return isActive;
}
public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
}


public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getRollNo() {
    return rollNo;
}
public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}

public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Date getRecordUpdatedDate() {
    return recordUpdatedDate;
}
public void setRecordUpdatedDate(Date recordUpdatedDate) {
    this.recordUpdatedDate = recordUpdatedDate;
}
public Date getRecordUpdatedTime() {
    return recordUpdatedTime;
}
public void setRecordUpdatedTime(Date recordUpdatedTime) {
    this.recordUpdatedTime = recordUpdatedTime;
}
public Date getRecordUpdatedOn() {
    return recordUpdatedOn;
}
public void setRecordUpdatedOn(Date recordUpdatedOn) {
    this.recordUpdatedOn = recordUpdatedOn;
}

}

在此处输入图片说明

I think you are problem with transaction. Can you change transaction code instead of yours.

Transaction transObj = null;
Session sessionObj = null;
try {
    sessionObj = HibernateUtil.buildSessionFactory().openSession();
    transObj = sessionObj.beginTransaction();
    transObj.commit();
} catch (HibernateException exObj) {
    if(transObj!=null){
        transObj.rollback();
    }
    exObj.printStackTrace(); 
} finally {
    sessionObj.close(); 
}

Because you are using

Session session = HibernateUtil.getCurrentSession();

but you dont have any currentSession . So this will throw NullPointerException when session.beginTransaction() ; calls. In my solution session is starting from BuildSessionFactory .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM