• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullPointerException when jsp accesses a Database

 
Princeton Ebanks
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building a web application that has a login page (jsp) and that attempts to check the user credentials by connecting to and searching for the username from a java db database. I use javabeans to initialize the form data and then call a function to validate the user.

The function works perfectly outside of the jsp file, but keeps giving a NullPoiunterException when called from the jsp.

Within the jsp, i issue the command:

<% if(loginHandler.validate(); %>

The class associated with the bean (loginHandler) is listed below:

public class VerifyLogin{
private String username;
private String password;
private String userDB;
private java.sql.Connection user_conn;
private String query_string;
private java.sql.Statement user_statement;
private java.sql.ResultSet user_credentials;

public VerifyLogin(){
username = null;
password = null;
userDB = null;
user_conn = null;
query_string = null;
user_statement = null;
user_credentials = null;
}

public String getUsername(){
return username;
}

public String getPassword(){
return password;
}

public void setPassword(String pw){
this.password = pw;
}

public void setUsername(String un){
this.username = un;
}


public boolean validate() throws java.sql.SQLException{

boolean valid_user = false;
userDB = "jdbc:derby://localhost:1527/OfficeAppsDB;user=admin;password=Passw0rd";
query_string = "SELECT * FROM ADMIN.USERS";

try{
Class.forName("org.apache.derby.jdbc.ClientDriver");
user_conn = java.sql.DriverManager.getConnection(userDB);
}
catch(java.sql.SQLException ex){
ex.printStackTrace();
}

catch (java.lang.Exception ex){
ex.printStackTrace();
}

try{
user_statement = user_conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
user_credentials = user_statement.executeQuery(query_string);
}
catch (java.sql.SQLException ex) {
ex.printStackTrace();
}

if(user_credentials.next()){
if(user_credentials.getObject("username").equals("paebanks")){
valid_user = true;
}
}
user_statement.close();
user_conn.close();

return valid_user;
}

}

The offending statement seems to be:

user_statement = user_conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);

This class works perfectly in a static context (from main), but keeps giving the exception when called from the jsp file. Can anyone shed some light on this?
[ March 20, 2008: Message edited by: Princeton Ebanks ]
 
Jeremy Wilson
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am wondering why you are not using Form based Authentication that is available as part of the web container. If using tomcat that are many examples on using a JDBC Realm for security.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Princeton,
This code snippet is masking the real error. If the code can't get an exception, it is logging the error and then going on - while the connection is null. Then in the next line, it is throwing a null pointer.



What is being output in the stack trace? This will give you a pointer as to what the real problem is with obtaining a connection. (My guess would be that the driver is not in the classpath of your server.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic