开发者

java.lang.nullpointer exception thrown in servlet program

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 !!


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


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.


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();
  }
}


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

Check what parameters are you getting there ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜