facebook

Hibernate configuration error

💡
Our Forums Have Moved

For help with installation, bugs reports or feature requests, please head over to our new forums.
Genuitec Community on GitHub

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #313262 Reply

    menyarito
    Member

    hello,
    I configure hibernate in myeclipse following the
    next tutorial:
    http://www.myeclipseide.com/documentation/quickstarts/hibernate/
    running the project it gives me an error message despite
    that data is properly recorded in the database
    error:

    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)
    

    my class Test_hibernate.java:

    package mypackage;
    
    import org.hibernate.Transaction;
    
    public class tesett {
    
        public static void main(String[] args) {
            addUser();
            listUser();
            changeUser();
        }
        
        private static void addUser() {
            User user = new User();
            user.setId(1);
            user.setUsername("jdoe");
            user.setPassword("1234");
            user.setFirstName("John");
            user.setLastName("Doe");
            user.setDateCreated(System.currentTimeMillis());
            
            UserDAO dao = new UserDAO();
            Transaction tx = dao.getSession().beginTransaction();        
            dao.save(user);        
            tx.commit();
            dao.getSession().close();
        }
        
        private static void listUser() {
            UserDAO dao = new UserDAO();
            User user = dao.findById(1);
            printUser("Printing User, ", user);
            dao.getSession().close();
        }
        
        private static void changeUser() {
            UserDAO dao = new UserDAO();
            User user = dao.findById(1);
            user.setUsername("jsmith");
            user.setPassword("abcd");
            user.setFirstName("Jane");
            user.setLastName("Smith");
            Transaction tx = dao.getSession().beginTransaction();
            dao.save(user);
            tx.commit();
            User updatedUser = dao.findById(1);
            printUser("Printing Updated User, ", updatedUser);
            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() + "]");
        }
    }
    

    thank you in advance

    #313268

    support-swapna
    Participant

    menyarito,

    I could not replicate it at my end.

    This should be due to a wrong value being passed to findById().
    Can you recheck if you are passing the proper id to the findById() for printUser?
    Also check if the dao.findById() is returning a valid user object or not.

    #313270

    menyarito
    Member

    super, thank you very much

Viewing 3 posts - 1 through 3 (of 3 total)
Reply To: Hibernate configuration error

You must be logged in to post in the forum log in