sql异常java.lang.NullPointerException

时间:2015-05-23 16:16:31

标签: java mysql servlets

我正在运行一个代码来存储MySQL数据库中的客户端注释和详细信息,并将它们显示在使用jsp设计的页面中 - 它生成:

  

Sql异常java.lang.NullPointerException

我正在粘贴代码供您参考,请帮我解决这个问题,同时我也会为您提供堆栈跟踪供您参考。

package com.message;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

public class AddMessageServlet extends HttpServlet {
private Connection con;
public void doGet(HttpServletRequest request,HttpServletResponse response )
    throws IOException,ServletException{
    response.setContentType("text/html;charset=utf-8");
    request.setCharacterEncoding("utf-8");
        String name=request.getParameter("name");
        String title=request.getParameter("title");
        String email=request.getParameter("email");
        String content=request.getParameter("content");
        if(name==null)
            name="";
        if(title==null)
            title="";
        if(email==null)
            email="";
        if(content==null)
            content="";
        try{
PreparedStatement 
stm=con.prepareStatement("insert into message values(?,?,?,?,?)");
            stm.setString(1, title);
            stm.setString(2, name);
stm.setDate
(3, new java.sql.Date(new java.util.Date().getTime()));                         
            if((email).length()==0)
                stm.setString(5, null);
            else stm.setString(5, email);
                stm.setString(4, content);
            try{
                stm.executeUpdate();

            }catch(Exception e){}
            RequestDispatcher dispatcher=
                    request.getRequestDispatcher("/ViewMessageServlet");
            dispatcher.forward(request, response);

        }catch(Exception e){
            e.printStackTrace();
        }
    }
    public void doPost(HttpServletRequest request,
   HttpServletResponse  response)
        throws IOException,ServletException{
    doGet(request,response);
}
public AddMessageServlet()
{
    String url="jdbc:mysql://localhost/liuyan";
    String userName="root";
    String password="neela123";
    String sql=null;
    con=null;
    Statement stmt=null;
    try{
        Class.forName("com.mysql.jdbc.Driver");

    }catch(ClassNotFoundException e){
        System.out.print("鍔犺浇椹卞姩寮傚父");
    }
    try{
        con=DriverManager.getConnection(url,userName,password);

    }catch(SQLException e){
        System.out.print("鍑虹幇SQLException寮傚父");
    }

}

}


 jsp code
    <%@page import="java.sql.*,com.message.Message,java.util.*" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
   pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     `
    `"http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>show the message in the table</title>
    </head>
    <body>
    <p align="center"><font size=5>所有留言</font></p>
    <hr>
    <%
    int message_count=0;
    Collection messages=(Collection)request.getAttribute("messages");
    Iterator it=messages.iterator();
    while(it.hasNext()){
    Message message=(Message)it.next();
    %>

    <table border=1 width=500 
align="center" cellpadding="3"   cellspacing="2">


<tr>
<td ><font size=3>留言人:</font></td>
<td><%=message.getName() %></td>
</tr>

    <tr>
    <td><font size=3>E-mail:</font></td>
    <td>
    <%
    out.println("<a href=mailto:"+message.getEmail()+">"
    +message.getEmail()+"</a>");
    %>
    </td>
    </tr>

    <tr>
    <td><font size=3>留言时间:</font></td>
    <td>
<%
    out.println
("<font size=3>"+message.getDate().toLocaleString()+"    </font>");

    %></td>

</tr>

<tr>
    <td><font size=3>留言内容:</font></td>
    <td><%=message.getContent()%></td>
</tr>
<%} %>
</table>


<p align="center"><a href="index.jsp">我要留言</a></p>
</body>
</html>

堆栈跟踪:

出现SQLException异常java.lang.NullPointerException
at com.message.AddMessageServlet.doGet(AddMessageServlet.java:33)
at com.message.AddMessageServlet.doPost(AddMessageServlet.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:0)

您是否检查过con.prepareStatement ...返回一个非空对象? 也许你的AddMessageServlet类中存在配置错误。

如果不是Servlet中第33行的内容?那是抛出异常的那一行

答案 1 :(得分:0)

我怀疑

stm.setString(5, null);我认为这不会被接受。 我会去

stm.setNull(5, java.sql.Types.STRING);