servlet程序中抛出java.lang.nullpointer异常

发布于 2024-10-23 21:39:39 字数 2295 浏览 1 评论 0原文

感谢大家的精彩回答。我尝试了@Jan、@Ratna 所说的一切,但我无法克服这个异常。我的 servlet 程序中的第 52 行是

Statement stmt=connect.createStatement();

我在这里展示更多的代码:

public class s3 extends HttpServlet
{
String message,seat_no,name,ans1,ans2,ans3,ans4,ans5;
int Total=0;
Connection connect;
Statement stmt=null;
ResultSet rs=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
String url="jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection(url,"root","root");
message="Connection Sucessfull";
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
catch(Exception excp){
excp.printStackTrace();
}
seat_no=request.getParameter("seat_no");
name=request.getParameter("name");
ans1=request.getParameter("group1");
ans2=request.getParameter("group2");
ans3=request.getParameter("group3");
ans4=request.getParameter("group4");
ans5=request.getParameter("group5");
if(Boolean.getBoolean(ans1) == true)
    Total+=2;
if(Boolean.getBoolean(ans2) == false)
    Total+=2;
if(Boolean.getBoolean(ans3) == true)
    Total+=2;
if(Boolean.getBoolean(ans4) == false)
    Total+=2;
if(Boolean.getBoolean(ans5) == false)
    Total+=2;
try
{
   if(Total>0)// Just a condition.. !
   {
    Statement stmt=connect.createStatement();
    String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
    stmt.executeUpdate(query);
    stmt.close();
  }
}
catch(SQLException ex){
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=pink>");
out.println("<h1>"+message+"</h1>\n");
out.println("<h3>database updated");
out.println("<br>");
out.println("<b>"+"datbase is follows");
out.println("<table border=5>");
try
{
Statement stmt=connect.createStatement();
String query="SELECT * FROM st1";
rs=stmt.executeQuery(query);
out.println("<th>"+"seat_no"+"</th>");
out.println("<th>"+"name"+"</th>");
out.println("<th>"+"marks"+"</th>");

您的宝贵建议将不胜感激!

Thanks for those wonderful answers everyone.I tried everything said by @Jan, @Ratna yet I am not able to overcome the exception. Line No.52 in my servlet program is

Statement stmt=connect.createStatement();

I present more of the code here:

public class s3 extends HttpServlet
{
String message,seat_no,name,ans1,ans2,ans3,ans4,ans5;
int Total=0;
Connection connect;
Statement stmt=null;
ResultSet rs=null;
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
try
{
String url="jdbc:mysql://localhost:3306/test";
Class.forName("com.mysql.jdbc.Driver");
connect=DriverManager.getConnection(url,"root","root");
message="Connection Sucessfull";
}
catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
catch(SQLException sqlex){
sqlex.printStackTrace();
}
catch(Exception excp){
excp.printStackTrace();
}
seat_no=request.getParameter("seat_no");
name=request.getParameter("name");
ans1=request.getParameter("group1");
ans2=request.getParameter("group2");
ans3=request.getParameter("group3");
ans4=request.getParameter("group4");
ans5=request.getParameter("group5");
if(Boolean.getBoolean(ans1) == true)
    Total+=2;
if(Boolean.getBoolean(ans2) == false)
    Total+=2;
if(Boolean.getBoolean(ans3) == true)
    Total+=2;
if(Boolean.getBoolean(ans4) == false)
    Total+=2;
if(Boolean.getBoolean(ans5) == false)
    Total+=2;
try
{
   if(Total>0)// Just a condition.. !
   {
    Statement stmt=connect.createStatement();
    String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
    stmt.executeUpdate(query);
    stmt.close();
  }
}
catch(SQLException ex){
}
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body bgcolor=pink>");
out.println("<h1>"+message+"</h1>\n");
out.println("<h3>database updated");
out.println("<br>");
out.println("<b>"+"datbase is follows");
out.println("<table border=5>");
try
{
Statement stmt=connect.createStatement();
String query="SELECT * FROM st1";
rs=stmt.executeQuery(query);
out.println("<th>"+"seat_no"+"</th>");
out.println("<th>"+"name"+"</th>");
out.println("<th>"+"marks"+"</th>");

Your valuable suggestions will be appreciated !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半步萧音过轻尘 2024-10-30 21:39:39

根据 MySQL 手册,您 需要调用

Class.forName("com.mysql.jdbc.Driver").newInstance();

在尝试打开连接之前 。您将因 forName() 调用而停止。

According to the MySQL manual, you need to call

Class.forName("com.mysql.jdbc.Driver").newInstance();

before trying to open a connection. You're stopping with the forName() call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文