感謝大家的那些精彩的答案。我嘗試了@Jan,@Ratna所說的一切,但我無法克服這個異常。在我的servlet程序中,第52行是在servlet程序中拋出的java.lang.nullpointer異常
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>");
您的寶貴建議可以理解的!
請發佈顯示錯誤的堆棧跟蹤 – 2011-03-20 05:41:09
請提供堆棧跟蹤。他們應該在日誌文件中。 – 2011-03-20 05:46:06
我使用MyEclipse Blue 8.6.1編譯程序,它有一個內置的MyEclipse Tomcat 6.0.13,沒有日誌。但拋出的異常是顯示java.lang.NullPointerException \t s3.doPost(s3.java:52) \t javax.servlet.http.HttpServlet.service(HttpServlet.java:710) \t javax.servlet.http.HttpServlet。服務(HttpServlet.java:803) – 2011-03-20 05:46:23