title: post jsp servlet array from jquery serialize array
body: <p>my problem i am not get array from jquery post to jspservlet is shows nullpointer exception always.my jsp page code is listed below :</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;script src="JS/jquery-latest.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(document).ready(function(){
    $("button").click(function(){
        $("p").fadeToggle(3000,function(){
            var fields=new Array();
  
            fields = $(":input").serializeArray();
            $.post("exampleservlet",{arraydata:fields,mode:"Insert"},
            function(data) {
                  alert("Data Loaded: " + data);
            });      
        });
        return false;
    });
  });
  &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form&gt;
          &lt;input type="text" name="txt_name" /&gt;
          &lt;input type="text" name="txt_name1" /&gt;
          &lt;input type="text" name="txt_name2" /&gt;
          &lt;input type="text" name="txt_name3" /&gt;
        &lt;p style="background:blue;color:white;padding:10px;"&gt;If you click on me, I will disappear.&lt;/p&gt;
          &lt;button&gt;Click me&lt;/button&gt;
      &lt;/form&gt;   
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and my jsp servlet java code listed below:</p>
  
  <pre><code>@Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          String jsresponse="No Message";
  
         try
          {
          String arrayData[] = request.getParameterValues("arraydata");
          //String data[]=request.getParameterValues("arraydata[]");
  
          jsresponse = arrayData[0] +" - ";//"This line shows null pointer exception!";
  
          }
          catch(Exception ex)
          {
  
          }
  
  
          response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
          response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
          response.getWriter().write(jsresponse);
      }
  </code></pre>
  
  <p>and i am getting null pointer exception on this line</p>
  
  <pre><code>jsresponse = arrayData[0];//"This line shows null pointer exception!";
  </code></pre>
  
  <p>it's shows error log like below:</p>
  
  <pre><code>Nov 15, 2012 4:10:03 PM org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [exampleservlet] in context with path [/javaservletwithajax] threw exception
  java.lang.NullPointerException
      at com.example.servlets.exampleservlet.doPost(exampleservlet.java:56)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      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:225)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:619)
  </code></pre>
  
  <p>any one help with me how to get array from dopost parameter values in jsp servlet!</p>
  
  <h2><strong>Solved My Problem from kmb385 like below:</strong></h2>
  
  <p><strong>Changes in jquery</strong></p>
  
  <pre><code>$(document).ready(function(){
    $("button").click(function(){
        $("p").fadeToggle(3000,function(){
  
             var fields=new Array();
             var values = new Array();
  
             fields = $(":input").serializeArray();
             $.each(fields, function(index,element){
               values.push(element.value);
             });
  
            $.post("exampleservlet",{arraydata:values,mode:"Insert"},
            function(data) {
                  alert("Data Loaded: " + data);
            });
  
        });
        return false;
    });
  });
  </code></pre>
  
  <p><strong>Changes in jsp servlet:</strong></p>
  
  <pre><code>@Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      String jsresponse="No Message";
  
     try
      {
      String arrayData[] = request.getParameterValues("arraydata[]");
      //String data[]=request.getParameterValues("arraydata[]");
      jsresponse = arrayData[0] +" - ";//"Test Response!";
  
      }
      catch(Exception ex)
      {
       jsresponse = ex.toString();               
      }
      response.setContentType("text/plain");  // Set content type of the response so that            jQuery knows what it can expect.
      response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
      response.getWriter().write(jsresponse);
  }
  </code></pre>
  
  <p>it's works cool!</p>