• Post Reply Bookmark Topic Watch Topic
  • New Topic

Null pointer exceptions in bean - help!  RSS feed

 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please could anyone tell what's wrong with this? I get null pointer exceptions in findUser() at these 2 lines:
userQuery = connection.prepareStatement(
"SELECT * FROM user where user_name = ?" );

userQuery.setString(1, userName);

I can't tell why. This is the code:

// JavaBean to store data for a user
package sonia.beans;
import java.sql.*;
import javax.servlet.http.*;
public class UserBean implements HttpSessionBindingListener
{
private String userName;
private String password;
private String country;
private String status;
private String bound;
private Connection connection = null;
private PreparedStatement userQuery = null;

public void findUser()
{
try
{
userQuery = connection.prepareStatement(
"SELECT * FROM user where user_name = ?"
);
}
catch (Exception e)
{
System.err.println("exception getting connection");
e.printStackTrace();
}
try
{
userQuery.setString(1, userName);
ResultSet rs = userQuery.executeQuery();
// get row data
if ( rs.next() )
{
setUserName( rs.getString( 1 ) );
setPassword( rs.getString( 2 ) );
setCountry( rs.getString( 3 ) );
setStatus( rs.getString( 4 ) );
}
}
catch (SQLException sqle)
{
System.err.println("exception getting row data");
sqle.printStackTrace();
}
catch (Exception e)
{
System.err.println("some exception with sql");
e.printStackTrace();
}
}

public void valueBound(HttpSessionBindingEvent be)
{
try
{
// load the driver
Class.forName( "org.gjt.mm.mysql.Driver").newInstance();
// connect to the database
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/quest1");
bound = "bound";
}
catch(Exception e)
{
System.err.println("Error connecting to database");
}
}

public void valueUnbound(HttpSessionBindingEvent be)
{
try
{
if (userQuery != null)
userQuery.close();
if (connection != null)
connection.close();
}
catch(Exception e)
{
System.err.println("problem closing");
e.printStackTrace();
}
}
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems pretty obvious that "connection" is null when your query is called. I notice that you don't have any diagnostic logging, so it's hard to tell why that might be. I suggest you at least put a System.out.println where "connection" is set and used, so you can look at the log and see what went on..
Is valueBound ever getting called ? You have remembered to tell the system that your class is listening for session events ? Are the connection details right - does getConnection() return nyull ? If connection is being set to something other than null, is there a long time between setting up the connection and using it - long enough for the database to expire the connection ?
 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for looking. Actually it worked when I booted up again this morning - the second time a problem solved itself after giving the computer a rest. Sorry for the unnecessary post. I'm wondering if my computer's a bit dodgy.
(There's no debugging in there because I tested a modified version with a main method and dummy valueBound/Unbound methods and found that it worked outside of JSP - probably a silly way of doing it but I'm learning..)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!