一个servlet抛出java.lang.NullPointerException的问题

insiderys 2005-04-25 07:14:33
我的提交页面的代码如下
<form name="form1" method="post" action="/servlet/BuyAction">
<input type="text" name="action"><br>
<input type="text" name="hw_id"><br>
<input type="text" name="hw_name"><br>
<input type="text" name="hw_cash"><br>
<input type="text" name="count"><br>
<input type="text" name="user_name"><br>
<input type="submit" name="Submit" value="提交">
</form>
我的servlet如下
package myclass;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.sql.*;

public class BuyAction extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
ResultSet rs;
faq2 mdb = null;
//String user_name=(String)getServletContext().getAttribute("user_name");
String action=request.getParameter("action");
String user_name=request.getParameter("user_name");
String hw_ids=request.getParameter("hw_id");
String counts=request.getParameter("count");
String hw_cashs=request.getParameter("hw_cash");
String hw_name=request.getParameter("hw_name");
//hw_name=new String(hw_name.getBytes("ISO8859-1"));
PrintWriter out=response.getWriter();
if (action.equals("buy"))
{
if(counts==null)
{
counts="1";
}
if(hw_cashs==null)
{
hw_cashs="1000";
}

if (hw_ids==null)
{
out.print("没有此货物!");
}

try
{
String sql="select * from basket where hw_id='"+hw_ids+"' and user_name='"+user_name+"' and basket_check=false";
rs=mdb.executeQuery(sql);
if(!rs.next())
{
sql="insert into basket(hw_id,user_name,basket_count,hw_name,hw_cash)";
sql=sql+"values('"+hw_ids+"','"+user_name+"','"+counts+"','"+hw_name+"','"+hw_cashs+"')";
mdb.executeInsert(sql);
}else
{
rs.previous();
String basket_count="0";
if(rs.next())
{
basket_count=rs.getString("basket_count");
}
int count=Cint(basket_count);
int duoyu=Cint(counts);
count=count+duoyu;
rs.previous();
if(rs.next()){
rs.updateString("basket_count",Integer.toString(count));
rs.updateRow();
}
}

}
catch(SQLException e)
{
e.printStackTrace();

}
response.sendRedirect("showing.jsp");
mdb.Close();}
}
private int Cint(String cint)
{
try {
int n;
n= Integer.parseInt(cint);
return n;
}
catch (NumberFormatException e) {
return 0;
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
doGet(request, response);
}

}
我的faq2代码如下
package myclass;


import java.sql.*;

public class faq2
{

String sDBDriver;
String sConnStr;
Connection conn;
ResultSet rs;

public faq2()
{
sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
sConnStr = "jdbc:odbc:shnxn";
conn = null;
rs = null;
try
{
Class.forName(sDBDriver);
}
catch(ClassNotFoundException classnotfoundexception)
{
System.err.println("faq(): " + classnotfoundexception.getMessage());
}
}

public void executeInsert(String s)
{
try
{
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement();
int i = statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("faq.executeUpdate:" + sqlexception.getMessage());
}
}

public ResultSet executeQuery(String s)
{
rs = null;
try
{
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement(1004, 1008);
rs = statement.executeQuery(s);
}
catch(SQLException sqlexception)
{
System.err.println("aq.executeQuery: " + sqlexception.getMessage());
}
return rs;
}

public void executeDelete(String s)
{
try
{
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement();
statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("faq.executeDelete: " + sqlexception.getMessage());
}
}

public int executeUpdate(String s)
{
int i = 0;
try
{
conn = DriverManager.getConnection(sConnStr);
Statement statement = conn.createStatement();
i = statement.executeUpdate(s);
}
catch(SQLException sqlexception)
{
System.err.println("faq.executeDelete: " + sqlexception.getMessage());
}
return i;
}

public void Close()
{
try
{
if(rs != null)
rs.close();
if(conn != null)
conn.close();
}
catch(SQLException sqlexception)
{
System.err.println("faq.executeDelete: " + sqlexception.getMessage());
}
}
}
编译都能通过,但在提交表单的时候会抛出异常
org.apache.jasper.JasperException
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:301)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:248)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)

有人知道这是为什么吗?
...全文
207 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaowenand 2005-04-25
  • 打赏
  • 举报
回复
对像为空,检查你的哪个对象值为空
menjew 2005-04-25
  • 打赏
  • 举报
回复
创建空对象了,看看rs有无问题?
MARS.nEIL 2005-04-25
  • 打赏
  • 举报
回复
rs为空?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧