• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

NullPointerException when jsp accesses a Database

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41967
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
I can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic