一个Struts中在Action中调用方法时出现的空指针错误,还请高手帮忙调试一下!!!

一个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.mysql.jdbc.ServerPreparedStatement.asSql(ServerPreparedStatement.java:338)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:504)
at com.mysql.jdbc.ServerPreparedStatement.toString(ServerPreparedStatement.java:2226)
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:411)
at com.mysql.jdbc.ServerPreparedStatement. <init>(ServerPreparedStatement.java:275)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4245)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4185)
at util.UserFind.find(UserFind.java:18)
at struts.action.LoginAction.execute(LoginAction.java:54)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:595)
-------->null
-------->null
补充:UserFind()方法我单独测试的时候能够从数据库中掉出来数据啊!可是在Action中就不行了!!!总是出现空指针。。。。。

-------->null
-------->null
说明你action中username和password都是null,为空。而你在查询中使用的sql语句
prepStmt = conn.prepareStatement("select userName,password "+"from user where userName=?");
prepStmt.setString(1,user.getName());

user中getName()为空,那么sql语句最终为 select userName,password from user where userName=;
这样的sql肯定不通过。

错误信息中,
at com.mysql.jdbc.Statement. <init>(Statement.java:190)
at com.mysql.jdbc.PreparedStatement. <init>(PreparedStatement.java:429)
at com.mysql.jdbc.ServerPreparedStatement.asSql(ServerPreparedStatement.java:338)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:504)
at com.mysql.jdbc.ServerPreparedStatement.toString(ServerPreparedStatement.java:2226)
at java.lang.String.valueOf(String.java:2577)
at java.lang.StringBuffer.append(StringBuffer.java:220)

也说明了由于你传递的参数为空,在解析sql语句的时候,将空对象转换成sql语句中的参数时出错。

建议,在查询前对username进行判断,如果username为空则不进行查询。