• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF database interaction  RSS feed

Adnan Gondal
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all!

Following bean's database related code is working in simple Java application but when i use it within JSF page it is giving Java null pointer exception error.
Thanks in advance.

Bean class:-

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Db implements Serializable{
int eId;
public int geteId() {
return eId;
public void seteId(int eId) {
this.eId = eId;
public static Connection getConnection() throws Exception {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbcracle:thin:@localhost:1521:globldb3";
String username = "scott";
String password = "tiger";
Connection conn = DriverManager.getConnection(url, username, password);
return conn;
public String addEmployee() throws Exception{
Connection conn = null;
PreparedStatement pstmt = null;
try {
int a = this.eId;
conn = getConnection();
String query = "INSERT INTO c(n) VALUES(?)";
pstmt = conn.prepareStatement(query);
pstmt.executeUpdate(); // execute insert statement
return "success";
} catch (Exception e) {
return "failure";
} finally {

Following is my JSF page

<html xmlns="http://www.w3.org/1999/xhtml"
<p>Enter value <h:inputText value="#{db.eId}"/> </p>
<p> <h:commandButton value="Add record" action="#{db.addEmployee}"/> </p>
Tim Holloway
Posts: 18662
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing really jumps out at me. Can you indicate which line the nullpointerexception occurs on?

Incidentally, there's a "Code" button on the message editor that can be used to wrap special tags around formatted data such as Java source code or XML. It keeps the message display from messing up your indentation.
Dieter Quickfend
Posts: 543
Java Netbeans IDE Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What line does the nullpointerexception occur? Is it when you create your preparedstatement? It could be your database driver/data.
Also, think you're supposed to use h:body in your facelets page. xml and doctype declaration? doubt that has anything to do with your problem though.
Guy deLyonesse
Ranch Hand
Posts: 200
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The private member is eId but your getter method is geteId().

The EL is probably looking for the public method getEId() since it automatically searches for a method name based on Camel case.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!