How to Solve Error Message null: org.postgresql.util.PSQLException: No value specified for parameter 1 when running Java Web Application using Primefaces Framework

Posted on

Introduction

This is another article showing an error message appear after accessing a Java web application running using Primefaces framework. The condition of the project is using the one available in several previous articles. The first one is the one which is describing about how to create a Java web application which exist in this link. It is an article with the title of ‘How to Create maven-based Web Application in NetBeans IDE’. Another one is an article with the title of ‘How to Add JSF Library to a maven-based Web Application in NetBeans IDE’ in this link. It informs how to add JSF library to the Java web application project. The last one is the article in this link with the title of ‘How to Add Primefaces Library to a maven-based Java Web Application in NetBeans IDE’. The content in that article is giving steps for adding Primefaces library functionality.

Furthermore, this article has a connection with the previous article where it is focusing on how to solve error message upon connecting to a database. Those articles are the one 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’ in this link. And the other one is the 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 edit URL Connection’ in this link.

Before going further, the following is the actual error message appear as the execution of the Java web application by submitting an entry form :

13:51:10,423 SEVERE [com.mycompany.web.view.BasicInputTextDBView] (default task-1) null: org.postgresql.util.PSQLException: No value specified for parameter 1.

There is an image showing the form for the sake of performing the entry name process as follows :

How to Solve Error Message null: org.postgresql.util.PSQLException: No value specified for parameter 1 when running Java Web Application using Primefaces Framework
How to Solve Error Message null: org.postgresql.util.PSQLException: No value specified for parameter 1 when running Java Web Application using Primefaces Framework

After typing the name in the textfield and click the Submit button, it will trigger a submit method available in the backing bean of the JSF file. In this context, the JSF file is ‘basic-input-text-db.xhtml’ with the following content :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/html"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
      <head>
            <title>TODO supply a title</title>
            <meta name="viewport" content="width-device-width, initial-scale=1.0"> 
      </head>
      <body>
            <div>TODO content</div>
            <div class="card">
                 <h:form>
                 <h5>Form User with only Basic InputText</h5>
                 <h5>Hello, <p:outputLabel for="username" value="#{basicInputTextDBView.username}"></p:outputLabel></h5>
                 <p:inputText id="username" value="#{basicInputTextDBView.username}"/>
                 <p:commandButton update="@form" actionListener="#{basicInputTextDBView.submit(){" value="Submit" />
                 </h:form> 
            </div>
            <div class="card">
                 <p:panel header="User List">
                    <p:dataTable var="user" value="#{userBean.userList}">
                        <p:column>
                           <f:facet name="header">
                              <h:outputText value="Username" />  
                           </f:facet>
                           <h:outputText value="#{user.username}" />  
                        </p:column>
                    </p:dataTable>
                 </p>
            </div>
      </body>
</html>

Continue on, the backing bean for the JSF file is actually a Java file with the name of ‘BasicInputTextDBView.java’. That file has the following content :

package com.mycompany.web.view;

import com.mycompany.web.bean.UserBean;
import com.mycompany.web.db.DBConnection;
import com.mycompany.web.model.User;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

/**
*
* @author Mark Spectre
*/
@Named(value = "basicInputTextDBView")
@ViewScoped
public class BasicInputTextDBView implements Serializable {

    private Connection connection;
    private Statement statement;
    private ResultSet resultSet;
    private final String TABLE_NAME = "employee";
    private User user;
    private List<User> userList;
    private String name;

    public BasicInputTextDBView() {
        connection = DBConnection.getConnection();
        System.out.println("Connection : "+connection);
        user = new User();
        userList = user.getUserList();
    }

    public String getName() {
       return name;
    }

    public void setName(String name) {
       this.name = name;
    }

    public void submit() {
       try {
             String query = "INSERT INTO " + TABLE_NAME
                            + "(name) "
                            + " values(?)";
             System.out.println(query);
             System.out.println("Name : "+name);
             connection = DBConnection.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(query);
             preparedStatement.execute();
             preparedStatement.close();
             preparedStatement.setString(1, name); //getName());
             User user = new User();
             user.setUsername(name);
             userList.add(user);
       } catch (SQLException ex) {
             Logger.getLogger(BasicInputTextDBView.class.getName()).log(Level.SEVERE, null, ex);
       }
    }
}

Solving the Error Message

Actually, the solution for the above error message which appear after the submit process is very simple. It is because there is a missing parameter upon the execution of the preparedStatement object. The above script has the execution of the preparedStatement object before there is an initialization for the first parameter of the query. In this context, it is a paramater for the name column. Before going on further, the query exist as follows :

     String query = "INSERT INTO " + TABLE_NAME
                      + "(name) "
                      + " values(?)";

The error message informing parameter 1 has not specified value. In the above context, it is the one marked with the ‘?’. It is the first parameter which is needed to be specified. Actually, there is a line which is specifying the value for parameter 1 as exist below :

preparedStatement.setString(1, name); //getName());

But the position exist after the execution and the closing or termination of the preparedStatement. That is the reason why the error message appear. In order to solve it, just change the sequence of the scripts. The correct sequence for solving the error message will be as follows :

PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, name); //getName());
preparedStatement.execute();
preparedStatement.close();

Build, compile and run the Java web application once more. If there are no more error, the the submit process for inserting the entry to the table available in the database will be a success.

One thought on “How to Solve Error Message null: org.postgresql.util.PSQLException: No value specified for parameter 1 when running Java Web Application using Primefaces Framework

Leave a Reply