2014-08-29 46 views
0

我是新來hibernate.When我試圖調用session.get()方法則空指針異常是occurring.How我可以修復它空指針異常而調用session.get()方法在Hibernate中

我POJO類: -

package test; 
import java.util.*; 

public class user1 implements java.io.Serializable{ 

private int id; 
private String name; 
private String department; 
private String password; 

public user1() {} 

public user1(int id,String name, String department, String password) { 
     this.id = id; 
     this.name = name; 
     this.department = department; 
     this.password = password; 
} 

public int getId() { 
    return id; 
} 

public void setId(int id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getDepartment() { 
    return department; 
} 

public void setDepartment(String department) { 
    this.department = department; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 

} 

我user1.hbm.xml文件:

<?xml version="1.0"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
<class name="test.user1" table="users"> 

<id name="id" column="id" type="int"> 
</id> 
<property name="name" column="name" type="string"/> 
<property name="department" column="department" type="string"/> 
<property name="password" column="password" type="string"/> 

</class> 
</hibernate-mapping> 

我打電話session.get(user1.class,ID)方法在我UpdateUser兩個()函數。運行時拋出Nullpointer異常。

public class user2 { 

private static SessionFactory factory; 
public static void main(String [] args){ 

    try{ 
     factory = new Configuration().configure().buildSessionFactory(); 

    } 
    catch (Throwable ex) { 
     System.err.println("SessionFactory object could not be created"+ex); 
     throw new ExceptionInInitializerError(ex); 
    } 

    user2 User = new user2(); 
    //User.addUser(3,"cc","cse","cc"); 
    //User.showUser(); 
    User.updateUser(Integer.valueOf(3),"dd","cse","dd"); 

} 

public void addUser(int id,String name,String department,String password){ 
    Session session = factory.openSession(); 
    Transaction t = null; 
    try{ 
     t = session.beginTransaction(); 
     user1 User1 = new user1(id,name,department,password); 
     session.save(User1); 
     t.commit(); 
    } 
    catch (HibernateException e) { 
     if (t!=null) t.rollback(); 
      e.printStackTrace(); 
    } 
    finally { 
     session.close(); 
    } 
} 



public void updateUser(Integer id,String name,String department,String password){ 
    Session session = factory.openSession(); 
    Transaction t = null; 
    try{ 
     user1 user = (user1)session.get(user1.class,id); 

     user.setName(name); 
     user.setDepartment(department); 
     user.setPassword(password); 
     session.update(user); 
     t.commit(); 
    } 
    catch (HibernateException e) { 
     if (t!=null) t.rollback(); 
      e.printStackTrace(); 
    } 
    finally { 
     session.close(); 
    } 

} 
} 

} 
+0

請加上異常堆棧跟蹤。在控制檯中顯示 – heikkim 2014-08-29 06:56:58

+0

錯誤: - 在螺紋>異常 「主」 顯示java.lang.NullPointerException \t在test.user2.updateUser(user2.java:87) \t在test.user2.main(user2.java:30) – user3921678 2014-08-29 07:03:44

+0

ID是我的數據庫的「用戶」表中的主鍵 – user3921678 2014-08-29 07:09:06

回答

0

你在你的updateUser(...)方法行t.commit();越來越NullPointerException,因爲您的交易變量t在行Transaction t = null;初始化爲null

所以才加入這一行中updaeUser方法來解決您的問題:

t = session.beginTransaction(); 
+0

它修正了錯誤。謝謝 – user3921678 2014-08-29 07:41:31