How to Solve Error Message Caused by: java.lang.NullPointerException: Cannot invoke “java.sql.Connection.prepareStatement(String)” because “this.connection” is null when executing Java Web Application using Primefaces Framework solve by edit URL Connection

Posted on

Introduction

This is the alternative continuation of the previous article. If in the previous article the error is still appear, the solution for solving it might be the wrong one. It can be the right one, but there is another part which is causing the same error. Actually, the previous article exist in this link. That article is an article with the title of ‘How to Solve Error Message Caused by: java.lang.NullPointerException: Cannot invoke “java.sql.Connection.prepareStatement(String)” because “this.connection” is null when executing Java Web Application using Primefaces Framework solve by adding Database Driver’. So, the previous article is proposing to solve the problem by adding a database driver. It is a PostgreSQL database driver since the connection from the Java web application is going to the PostgreSQL database. On the other hand, this article will discuss a little bit more further if adding the database driver definition is not working at all.

As usual, before going on further, the error message appear in this article exist in a running Java web application. There are several settings of that Java web application exist in the previous article. The first one is for creating the actual Java web application project based on maven on this link. The title of that article is ‘How to Create maven-based Web Application in NetBeans IDE’. The second one is adding the JSF framework to the project which exist in this link. That article has the title of ‘How to Add JSF Library to a maven-based Web Application in NetBeans IDE’. The last one is an article with the title of ‘How to Add Primefaces Library to a maven-based Java Web Application in NetBeans IDE’ in this link for adding the Primefaces framework usability to the project.

So, after adding the definition of the PostgreSQL database driver definition to solve the problem as exist in the article in this link, the error message still appear. It is an article with the title of ‘How to Solve Error Message Caused by: java.lang.NullPointerException: Cannot invoke “java.sql.Connection.prepareStatement(String)” because “this.connection” is null when executing Java Web Application using Primefaces Framework solve by adding Database Driver’.

Solving the Error Message

It is another solution for solving the error message other than adding database driver definition in the ‘pom.xml’ file. It is checking the URL database connection in the Java web application itself. Actually, the URL database connection is available in the Java file with the name of ‘DBConnection.java’. Below is the actual content of the ‘DBConnection.java’ file :

package com.mycompany.web.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Mark Spectre
*/
public class DBConnection {

    private static String url, user, password;

    static void init() {
        url = "postgresql://localhost:5432/employee"; 
        user = "postgres"; 
        password = "password"; 
    }

    public static Connection getConnection() {
       init();

       try {
           return DriverManager.getConnection(url, user, password);
       } catch (SQLException ex) {
           System.out.println("Error Connection");
           Logger.getLogger(DBConnection.class.getName()).log(Level.SEVERE, null, ex);
           ex.printStackTrace();
           return null;
       }
    }
}

Apparently, after going through some searching, there is something wrong with the URL database connection. That is why finding the database driver ends in failure. The URL connection is false in the above script. In the above script, the variable with the name of ‘url’ has a value of ‘postgresql://localhost:5432/employee’. It will access a PostgreSQL database in the localhost machine address, through port 5432 to the database with the name of ’employee’. But there is something missing with that URL database connection pattern. The correct one must be preceeded by ‘jdbc’ keyword. For more information about the URL connection pattern using JDBC connection, just check this link. So, in order to solve the problem, just change the url variable as follows :

    static void init() {
        url = "jdbc:postgresql://localhost:5432/employee"; 
        user = "postgres"; 
        password = "password"; 
    }

One thought on “How to Solve Error Message Caused by: java.lang.NullPointerException: Cannot invoke “java.sql.Connection.prepareStatement(String)” because “this.connection” is null when executing Java Web Application using Primefaces Framework solve by edit URL Connection

Leave a Reply