File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes JSF database interaction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "JSF database interaction" Watch "JSF database interaction" New topic

JSF database interaction

Adnan Gondal
Ranch Hand

Joined: Apr 30, 2011
Posts: 35
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 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=""
<p>Enter value <h:inputText value="#{db.eId}"/> </p>
<p> <h:commandButton value="Add record" action="#{db.addEmployee}"/> </p>
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

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.

An IDE is no substitute for an Intelligent Developer.
Dieter Quickfend

Joined: Aug 06, 2010
Posts: 543

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.

Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

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.
I agree. Here's the link:
subject: JSF database interaction
jQuery in Action, 3rd edition