-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: NullPointerException displayed when adding records in table
PostPosted: Wed Jan 06, 2016 3:41 am 
Newbie

Joined: Wed Jan 06, 2016 3:33 am
Posts: 2
Please Help: Below error when I tried to add details to tables using hibernate:

Code:
java.lang.NullPointerException
   org.hibernate.cache.impl.bridge.RegionFactoryCacheProviderBridge.nextTimestamp(RegionFactoryCacheProviderBridge.java:93)
   org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:639)
   org.hibernate.impl.SessionFactoryImpl.openSession(SessionFactoryImpl.java:648)
   com.package1.service.AuthenticateUser.addUser(AuthenticateUser.java:32)
   com.package1.controllers.LoginServlet.doPost(LoginServlet.java:68)


LoginServlet
Code:
public class LoginServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   protected void doPost(HttpServletRequest request,
         HttpServletResponse response) throws ServletException, IOException {
      PrintWriter out = response.getWriter();
      String username = request.getParameter("user");
      String password = request.getParameter("pass");

      response.setContentType("text/html");
      String result = authenticateUser.authenticateUser(username, password);

      if (result == "SUCCESS") {
         RequestDispatcher rqdpr = request.getRequestDispatcher("Success.jsp");
         rqdpr.forward(request, response);
      }else {
         //response.sendRedirect("Register.jsp");
         authenticateUser.addUser("abcdef", "abcdef","abcdef", "abcdef");   //Line: 68
      }
      
      
   }
   
   private AuthenticateUser authenticateUser = new AuthenticateUser();
}


AuthenticateUser
Code:
public class AuthenticateUser {

   public String authenticateUser(String username, String password) {
      Session session = factory.openSession();
      Transaction txn = session.getTransaction();
      String usernamefromdb = user.getName();
      String passwordfromdb = user.getPassword();

      try {
         if (username.equals(usernamefromdb)
               && password.equals(passwordfromdb)) {
            return "SUCCESS";
         } else {
            return "FAILURE";
         }
      } finally {
         factory.close();
      }
   }

   public void addUser(String uname, String uemail, String usrnme,
         String upass) {
      Session session = factory.openSession();                       // Line 32
      Transaction txn = session.beginTransaction();
      user.setName(uname);
      user.setEmail(uemail);
      user.setUsrname(usrnme);
      user.setPassword(upass);
      txn.commit();
      session.save(user);
      session.close();
      factory.close();
   }

   private static SessionFactory factory = HibernateSessionManager
         .getSessionFactory();
   private User user = new User();
}


Top
 Profile  
 
 Post subject: Re: NullPointerException displayed when adding records in table
PostPosted: Wed Jan 06, 2016 3:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That RegionFactoryCacheProviderBridge is from Hibernate 3, right?
I see lots of others problems related to your code:

- you close the SessionFactory after each operation
- you use the session-per-operation anti-pattern
- you don't close the Session in a final block, so if you get an exception the Session or the database connection might not be closed properly

Did you set a Caching provider?

If you set this:

Code:
<property name="hibernate.cache.use_second_level_cache">true</property>


You should also set a provider:

Code:
<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>


Also worth checking:

https://forum.hibernate.org/viewtopic.php?f=1&t=996962


Top
 Profile  
 
 Post subject: Re: NullPointerException displayed when adding records in table
PostPosted: Wed Jan 06, 2016 5:24 am 
Newbie

Joined: Wed Jan 06, 2016 3:33 am
Posts: 2
I made below changes
Code:
public class AuthenticateUser {

   public String authenticateUser(String username, String password) {
      
      return "ABC";
   }

   public void addUser(String uname, String uemail, String usrnme,
         String upass) {
      Session session = factory.openSession();
      Transaction txn = session.beginTransaction();
      user.setId(1);
      user.setName(uname);
      user.setEmail(uemail);
      user.setUsrname(usrnme);
      user.setPassword(upass);
      txn.commit();
      session.save(user);
      session.close();
      factory.close();
   }

   private static SessionFactory factory = HibernateSessionManager
         .getSessionFactory();
   private User user = new User();
}


Now the error vanished, but table is not created. Below is

hibernate-cfg.xml
Code:
<hibernate-configuration>

     <session-factory>

        <!-- Database connection settings -->
        <property name="connection.driver_class">org.postgresql.Driver</property>
        <property name="connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property>
        <property name="connection.username">postgres</property>
        <property name="connection.password">admin</property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">Update</property>

        <!-- Names the annotated entity class -->
        <mapping class="com.package1.model.User"/>

    </session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject: Re: NullPointerException displayed when adding records in table
PostPosted: Wed Jan 06, 2016 6:03 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That's because instead of:

Code:
<property name="hbm2ddl.auto">Update</property>


you should have:

Code:
<property name="hbm2ddl.auto">update</property>


or

Code:
<property name="hbm2ddl.auto">create-drop</property>


The configuration properties are case-sensitive.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.