一个Struts中在Action中调用方法时出现的空指针错误,还请高手帮忙调试一下!!!
代码如下:
//UserFind方法,根据表单数据查找用户
package util;
import java.sql.*;
import entity.User;
import util.JdbcUtil1;
public class UserFind {
public User find(User user){
Connection conn = null;
PreparedStatement prepStmt=null;
ResultSet rs = null;
User u = new User();
try {
conn = JdbcUtil1.getConnection();
prepStmt = conn.prepareStatement("select userName,password "+"from user where userName=?");
prepStmt.setString(1,user.getName());
rs = prepStmt.executeQuery();
if(rs.next()){
String DbName = rs.getString(1);
String DbPassword = rs.getString(2);
u.setName(DbName);
u.setPassword(DbPassword);
}
System.out.println(u.getName());
System.out.println(u.getPassword());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtil1.release(rs, prepStmt, conn);
}
return u;
}
/*public static void main(String[] args) throws SQLException{
User user = new User();
user.setName("tite");
user.setPassword("tie");
boolean b = find(user);
System.out.println(b);
}*/
}
Action方法
public class LoginAction extends Action {
/*
* Generated Methods
*/
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
* @throws SQLException
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response){
LoginForm loginForm = (LoginForm) form;// TODO Auto-generated method stub
String userName = loginForm.getUserName();
String password = loginForm.getPassword();
User user = new User();
user.setName(userName);
user.setPassword(password);
System.out.println("-------->"+user.getName());
System.out.println("-------->"+user.getPassword());
try {
User u = (new UserFind()).find(user);
System.out.println("-------->"+u.getName());
System.out.println("-------->"+u.getPassword());
if(u.getName()!=null){
if(user.getPassword().equals(u.getPassword())){
HttpSession session = request.getSession();
session.setAttribute("user",user);
return mapping.findForward("list1");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return mapping.findForward("login");
}
return mapping.findForward("login");
}
}
出现的异常:
on Aug 04 14:16:21 CST 2008 TRACE:
java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:562)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:504)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:3228)
at java.lang.String.valueOf(String.java:2577)
at java.lang.StringBuffer.append(StringBuffer.java:220)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:190)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:429)
at com.m