servlet程序中抛出java.lang.nullpointer异常
我正在为只有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下文件 s3.java,第 52 行。
Take a look at the file s3.java, line 52.
我的猜测是,您通过
getParameter
检索的字段之一不存在,因此它在null
中。当你在该字段上调用 .equals()
时,你会得到 NPE。如果我们知道哪一行是 #52 将会有所帮助。作为第一步,我将重写所有检查,例如:
或什
至更好
然后,即使请求中未提供该字段,您也不会得到 NPE。
My guess is that one of the fields you are retrieving via
getParameter
is not there so it innull
. 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:
to
or even better
Then you will not get NPE even if the field was not presented in the request.
@Jan Zyka 的支持答案
试试这个
supporting answer from @Jan Zyka
Try this
我认为您可能会在文件 s3.java 第 52 行以任何带有
NULL
的参数结束。检查您获得了哪些参数?
I think you might be ending with any parameter with
NULL
at file s3.java, line 52Check what parameters are you getting there ?