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

发布于 2024-10-22 11:49:34 字数 1841 浏览 0 评论 0原文

我正在为只有 5 个问题的在线考试创建一个 3 层 Web 应用程序。我使用 html 并设计了一个 servlet 将数据发布到 mysql 数据库,我将其命名为“test”,表为“st1”。我使用 MyEclipse Blue 8.6.1,它有一个内置的 MyEclipse Tomcat 6.0.13。 doPost() 方法中 servlet 的编码如下:

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(ans1.equals("True"))
    Total+=2;
if(ans2.equals("False"))
    Total+=2;
if(ans3.equals("True"))
    Total+=2;
if(ans4.equals("False"))
    Total+=2;
if(ans5.equals("False"))
    Total+=2;
try
{
    Statement stmt=connect.createStatement();
    String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
    stmt.executeUpdate(query);
    stmt.close();
}

当我编译应用程序时,我不断收到这样的异常:

HTTP Status 500 - 

type Exception report
message: 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception :
java.lang.NullPointerException
    s3.doPost(s3.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note :The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs

但我找不到任何适用于 MyEclipse Tomcat 6.0.13 的日志。谁能帮助我克服这个异常?这是我的学术项目!提前致谢 !!

I am creating a 3-tier web application for an online exam having just 5 questions.I use html and have designed a servlet to post data to the mysql database which I have named as 'test' and the table is 'st1'. I use MyEclipse Blue 8.6.1 which has an inbuilt MyEclipse Tomcat 6.0.13. The coding for servlet in doPost() method is as follows:

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(ans1.equals("True"))
    Total+=2;
if(ans2.equals("False"))
    Total+=2;
if(ans3.equals("True"))
    Total+=2;
if(ans4.equals("False"))
    Total+=2;
if(ans5.equals("False"))
    Total+=2;
try
{
    Statement stmt=connect.createStatement();
    String query="INSERT INTO st1("+"seat_no,name,marks"+")VALUES('"+seat_no+"','"+name+"','"+Total+"')";
    stmt.executeUpdate(query);
    stmt.close();
}

When I compile the application, I keep getting an exception as such:

HTTP Status 500 - 

type Exception report
message: 
description The server encountered an internal error () that prevented it from fulfilling this request.
exception :
java.lang.NullPointerException
    s3.doPost(s3.java:52)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note :The full stack trace of the root cause is available in the Apache Tomcat/6.0.13 logs

But I could not find any logs available for MyEclipse Tomcat 6.0.13. Can anyone help me overcome this exception? Its my academic project ! Thanks in Advance !!

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

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

发布评论

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

评论(4

如果没结果 2024-10-29 11:49:34

看一下文件 s3.java,第 52 行。

Take a look at the file s3.java, line 52.

人疚 2024-10-29 11:49:34

我的猜测是,您通过 getParameter 检索的字段之一不存在,因此它在 null 中。当你在该字段上调用 ​​.equals() 时,你会得到 NPE。如果我们知道哪一行是 #52 将会有所帮助。

作为第一步,我将重写所有检查,例如:

ans1.equals("True")

或什

"true".equalsIgnoreCase(ans1)

至更好

Boolean.getBoolean(ans1) == true

然后,即使请求中未提供该字段,您也不会得到 NPE。

My guess is that one of the fields you are retrieving via getParameter is not there so it in null. Whend you call .equals() on that field you get NPE. If we know which line is #52 it would help.

As a first step I would rewrite all the checks which are like:

ans1.equals("True")

to

"true".equalsIgnoreCase(ans1)

or even better

Boolean.getBoolean(ans1) == true

Then you will not get NPE even if the field was not presented in the request.

不羁少年 2024-10-29 11:49:34

@Jan Zyka 的支持答案

试试这个

if("True".equals(ans1))
    Total+=2;
if("False".equals(ans2))
    Total+=2;
if("True".equals(ans3))
    Total+=2;
if("False".equals(ans4))
    Total+=2;
if("False".equals(ans5))
    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();
  }
}

supporting answer from @Jan Zyka

Try this

if("True".equals(ans1))
    Total+=2;
if("False".equals(ans2))
    Total+=2;
if("True".equals(ans3))
    Total+=2;
if("False".equals(ans4))
    Total+=2;
if("False".equals(ans5))
    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();
  }
}
酒中人 2024-10-29 11:49:34

我认为您可能会在文件 s3.java 第 52 行以任何带有 NULL 的参数结束。

检查您获得了哪些参数?

I think you might be ending with any parameter with NULL at file s3.java, line 52

Check what parameters are you getting there ?

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