How to Solve Error Message NullPointerException on creating SessionFactory to get Database Connection in Java Application

Posted on

Introduction

In this article, the main focus is to be able to solve a NullPointerException message. In this case, there is a running Java Program where the process begin with series of building, compiling and deployment. The process for building, compiling and deploying the application is using NetBeans IDE. There is an internal maven tool for building, compiling and deploying the Java Application. For the deployment process, it is using Wildfly Application Server to run the Java Application. But in this context, the error message appear upon executing the Java application itself. In other words, the process for deploying the Java application is already over. But upon accessing a menu in the Java application, the error message appear in the output Tab of the NetBeans IDE. The following are the error message :

2022-02-20 19:04:46,045 ERROR [stderr] (default task-2) java.lang.NullPointerException
2022-02-20 19:04:46,045 ERROR [stderr] (default task-2)     at com.app.service.AdminService.getMenu(AdminService.java:54)
2022-02-20 19:04:46,045 ERROR [stderr] (default task-2)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2022-02-20 19:04:46,045 ERROR [stderr] (default task-2)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

So, after searching in the AdminService java file, specifically in line 54, there is a line as follows :

Solution

Actually, it is very simple for working the solution. The error message appear already hinting the cause of the error. There is a null value exist which is causing the error. So, in the AdminService.java in line 54, the following snippet code actually which is causing the error message :

session = sqlSessionFactory.openSession();

Tracing further, the above snippet code has a definition of an object variable with the type of SQLSessionFactory before. But executing the ‘openSession()’ method does not bring or create the instance of SQLSessionFactory into the ‘session’ variable.

import org.apache.ibatis.session.SqlSessionFactory;
@Stateless
public class AdminService {
       private SqlSessionFactory sqlSessionFactory;
       @PostConstruct
       public void init() {
           try {
               sqlSessionFactory = MyBatisConnectionFactory.getSqlSessionFactory();
           } catch (Exception ex) {
               ex.printStackTrace();
           }
       }
       ...
       ...
       ...
       public List<MenuReference> getMenuReferences() throws Exception {
           try {
               session = sqlSessionFactory.openSession();
               return dao.getMenuReferences(session);
           } catch (Exception e) {
               e.printStackTrace();
               throw new Exception(e.getMessage());
           } finally {
               if (session != null) {
                   session.close();
           }
       }
}

Actually, the above snippet code is just a part of the AdminService.java file. It is only showing the important part of the code for the main purpose of solving the problem. The first one is the ‘init’ method and the second one is ‘getMenuReferences(). The line which is causing the error message exist in the ‘getMenuReferences()’ method. As it exist in the above :

import org.apache.ibatis.session.SqlSessionFactory;

So, the one which is triggering the NullPointerException is the ‘session’ variable exist in ‘getMenuReferences()’. Apparently, in the init method, generating the sqlSessionFactory ends in failure. The cause of the failure is the configuration for generating the sqlSessionFactory is wrong. The location for the sqlSessionFactory exist in ‘src/main/resources/application.properties’. The following is the content of it :

dbServer=localhost
dbName=product
dbPort=5432
dbDriver=com.postgresql.jdbc.Driver

Unfortunately, the above configuration is wrong. Since it is an attempt for moving the database from MySQL to PostgreSQL, the only modification is in the ‘dbPort’ and also the ‘dbDriver’. The following is the original content of the ‘application.properties’ for using the MySQL database server :

dbServer=localhost
dbName=product
dbPort=3306
dbDriver=com.mysql.jdbc.Driver

Without having second thought, just change the dbPort from ‘3306’ to ‘5432’ and the dbDriver from ‘com.mysql.jdbc.Driver’ to ‘com.postgresql.jdbc.Driver’. After searching through, the PostgreSQL database driver name is wrong. That is the cause of the NullPointerException. It cannot generate the sqlSessionFactory object variable because the driver name of PostgreSQL database is wrong. After searching and googling through several articles, the answer exist in the article exist in this link. It is an article with the title of ‘Some configurations of mybatis + PostgreSQL + maven’. According to the article, the correct name for the PostgreSQL driver using PostgreSQL driver from maven repository is ‘org.postgresql.Driver’. For the definition of the PostgreSQL driver package in the ‘pom.xml’ to retrieve it from maven repository exist as follows :

The correct one for PostgreSQL exist as follows :

<dependency>
   <groupId>org.postgresql</groupId>
   <artifactId>postgresql</artifactId>
   <version>42.3.3</version>
</dependency>

Modifying the ‘application.properties’, especially the PostgreSQL database driver name as follow as an attempt to solve the problem :

dbServer=localhost
dbName=product
dbPort=5432
dbDriver=org.postgresql.Driver

After changing it and then building, compiling and deploying the Java application, the error is finally solved.

Leave a Reply