title: Sql exception java.lang.NullPointerException
body: <p>I am running a code to store client comments and details in MySQL database and display them in a page designed using jsp - it generates:</p>
  
  <blockquote>
    <p>Sql exception java.lang.NullPointerException </p>
  </blockquote>
  
  <p>I am pasting the code for your reference, please help me to fix this issues, and also I am providing you the stack trace for your reference.</p>
  
  <pre><code>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
      &lt;%@page import="java.sql.*,com.message.Message,java.util.*" %&gt;
      &lt;%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%&gt;
      &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"     `
      `"http://www.w3.org/TR/html4/loose.dtd"&gt;
      &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;show the message in the table&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
      &lt;p align="center"&gt;&lt;font size=5&gt;所有留言&lt;/font&gt;&lt;/p&gt;
      &lt;hr&gt;
      &lt;%
      int message_count=0;
      Collection messages=(Collection)request.getAttribute("messages");
      Iterator it=messages.iterator();
      while(it.hasNext()){
      Message message=(Message)it.next();
      %&gt;
  
      &lt;table border=1 width=500 
  align="center" cellpadding="3"   cellspacing="2"&gt;
  
  
  &lt;tr&gt;
  &lt;td &gt;&lt;font size=3&gt;留言人：&lt;/font&gt;&lt;/td&gt;
  &lt;td&gt;&lt;%=message.getName() %&gt;&lt;/td&gt;
  &lt;/tr&gt;
  
      &lt;tr&gt;
      &lt;td&gt;&lt;font size=3&gt;E-mail:&lt;/font&gt;&lt;/td&gt;
      &lt;td&gt;
      &lt;%
      out.println("&lt;a href=mailto:"+message.getEmail()+"&gt;"
      +message.getEmail()+"&lt;/a&gt;");
      %&gt;
      &lt;/td&gt;
      &lt;/tr&gt;
  
      &lt;tr&gt;
      &lt;td&gt;&lt;font size=3&gt;留言时间：&lt;/font&gt;&lt;/td&gt;
      &lt;td&gt;
  &lt;%
      out.println
  ("&lt;font size=3&gt;"+message.getDate().toLocaleString()+"    &lt;/font&gt;");
  
      %&gt;&lt;/td&gt;
  
  &lt;/tr&gt;
  
  &lt;tr&gt;
      &lt;td&gt;&lt;font size=3&gt;留言内容：&lt;/font&gt;&lt;/td&gt;
      &lt;td&gt;&lt;%=message.getContent()%&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;%} %&gt;
  &lt;/table&gt;
  
  
  &lt;p align="center"&gt;&lt;a href="index.jsp"&gt;我要留言&lt;/a&gt;&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h3>Stack trace:</h3>
  
  <pre><code>出现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)
  </code></pre>