erreur ds projet hibernate
Bonsoir,
dans mon projet hibernate qui tourne sous myeclipse,
j'ai une nouvelle erreur qui s'affiche malgré que je n'ai rien modifié
et qu'avant tout fonctionne bien :
Code:
1 2 3 4
| Exception in thread "main" java.lang.NullPointerException
at modele.Test.printUser(Test.java:96)
at modele.Test.listUser(Test.java:55)
at modele.Test.main(Test.java:15) |
malgré l'erreur les données sont enregistrés dans la base de donnée
Test.java:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package com.myeclipse.hibernate;
import org.hibernate.Transaction;
public class HibernateExample {
/**
* @param args
*/
public static void main(String[] args) {
// 1. Add the new user
addUser();
// 2. Retrieve the user and print it
listUser();
// 3. Change the user record and update it
changeUser();
}
private static void addUser() {
// 1. Create user
User user = new User();
user.setId(1);
user.setUsername("jdoe");
user.setPassword("1234");
user.setFirstName("John");
user.setLastName("Doe");
user.setDateCreated(System.currentTimeMillis());
// 2. Create DAO
UserDAO dao = new UserDAO();
// 3. Start the transaction
Transaction tx = dao.getSession().beginTransaction();
// 4. Add user
dao.save(user);
// 5. Commit the transaction (write to database)
tx.commit();
// 6. Close the session (cleanup connections)
dao.getSession().close();
}
private static void listUser() {
// 1. Create DAO
UserDAO dao = new UserDAO();
// 2. Find user by ID
User user = dao.findById(1);
// 3. Print the user information out
printUser("Printing User, ", user);
// 4. Close the session (cleanup connections)
dao.getSession().close();
}
private static void changeUser() {
// 1. Create DAO
UserDAO dao = new UserDAO();
// 2. Find user by ID
User user = dao.findById(1);
// 3. Change user information
user.setUsername("jsmith");
user.setPassword("abcd");
user.setFirstName("Jane");
user.setLastName("Smith");
// 4. Start the transaction
Transaction tx = dao.getSession().beginTransaction();
// 5. Update the user record with the changes
dao.save(user);
// 6. Commit the transaction (write to database)
tx.commit();
// 7. Load the updated user from the database
User updatedUser = dao.findById(1);
// 8. Print the updated user information out to confirm the changes
printUser("Printing Updated User, ", updatedUser);
// 9. Close the session (cleanup connections)
dao.getSession().close();
}
private static void printUser(String extraText, User user) {
System.out.println(extraText
+ " User[Username: "
+ user.getUsername()
+ ", Password: "
+ user.getPassword()
+ ", First Name: "
+ user.getFirstName()
+ ", Last Name: "
+ user.getLastName() + "]");
}
} |
Merci d'avance pour votre aide