0

I have a website and I used form inside .jsp file when the user submit the form the action goes to servlet which insert data in database. some times it works fine without any problem and sometimes even the user submit the form nothing inserted in the database.

Any one could advice me please what is the problem.

    24-Aug-2017 01:58:43.958 SEVERE [http-nio-13214-exec-3] 
    org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for 
    servlet [NewServlet] in context with path [] threw exception
    java.lang.NullPointerException
    at org.example.test.NewServlet.processRequest(NewServlet.java:154)
    at org.example.test.NewServlet.doGet(NewServlet.java:389) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFil
   terChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

    24-Aug-2017 01:58:50.928 SEVERE [http-nio-13214-exec-9] 
    org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for 
    servlet [NewServlet] in context with path [] threw exception
    java.lang.NullPointerException
    at org.example.test.NewServlet.processRequest(NewServlet.java:154)
    at org.example.test.NewServlet.doGet(NewServlet.java:389)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

index.jsp

<form action="NewServlet" method="post" onsubmit="myFunction();">
<table>
<tbody>
<tr>
<td><sql:query dataSource="${snapshot}" var="subjects1">
SELECT * FROM glf2 ORDER BY RAND()LIMIT 1
</sql:query>
<c:set var="comments_m1" value="${subjects1.rows[0]}"/>
${comments_m1.comment_message} 
</td>
</tr>
<tr>
<td><sql:query dataSource="${snapshot}" var="subjects2">
SELECT * FROM egy ORDER BY RAND()LIMIT 1
</sql:query>
<c:set var="comments_m2" value="${subjects2.rows[0]}"/>
${comments_m2.comment_message} 
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="com" value="${comments_m1.comment_message}"/>
<input type="hidden" name="com" value="${comments_m2.comment_message}"/>
<button onclick="submit">submit</button>  
</form> 
<script>
function myFunction()
{ check all filleds are selected}
</script>

the comment message selected from database and shown in the screen and the user need to select some options and the comment message and the user options then be inserted into database table.

newservlet.java acording to the log file the problem in getParametersValues

try (PrintWriter out = resp.getWriter()) {
comment1= req.getParameterValues("com");
for(int z=0; z<comment1.length;z++)
comment[z] = new String(comment1[z].getBytes("ISO-8859-1"),"UTF-8");
Areej
  • 23
  • 5
  • 1
    what is present at NewServlet.java:154? null pointer exception is thrown there. provide the snippet of your code. – sanjiv saini Aug 24 '17 at 16:22
  • @sanju singh just a for loop 152 try (PrintWriter out = resp.getWriter()) { 153 comment1= req.getParameterValues("com"); 154 for(int z=0; z – Areej Aug 24 '17 at 17:38
  • why sometimes works without any problem? – Areej Aug 24 '17 at 17:41
  • thats because req.getParameterValues("com") is returning null sometimes, when it don't find value. can you system out comment1 after line 153 and check? – sanjiv saini Aug 24 '17 at 17:46
  • but the values of comment1 coming from .jsp as a result of select statement and I am sure it is not null because it is shown in the screen. – Areej Aug 24 '17 at 17:57
  • ok I will try and back to you. Thank you – Areej Aug 24 '17 at 18:22
  • I tried nothing printed. maybe because it is a servlet.java mo main method!? – Areej Aug 24 '17 at 18:39
  • that doesn't matter. It didn't print anything because request had no parameters. you can check the network tab of chrome to see what parameter are being sent. – sanjiv saini Aug 24 '17 at 18:46
  • System.out.println(request.getParameterMap().keySet()). check by adding this at 153. – sanjiv saini Aug 24 '17 at 18:53
  • I added the statement and checked network tab no parameters. – Areej Aug 24 '17 at 22:33
  • so, thats the reason, you are not getting any parameter with name 'com' hence getParameterValues() return null, and trying to access from null ( here length) causes null pointer exception. – sanjiv saini Aug 25 '17 at 02:45
  • but why sometimes it gets values and insert into database without any problem. I don't know how to solve the problem because it works some times. – Areej Aug 25 '17 at 10:45
  • provide your client side code by editing the question – sanjiv saini Aug 25 '17 at 11:38
  • do you think it id something related to the connection, I closed all connection after insertion not in finally clause. – Areej Aug 25 '17 at 11:42
  • I added the client side code. – Areej Aug 25 '17 at 11:59
  • do you think that the problem with scope https://stackoverflow.com/questions/5027160/jsp-usebean-is-null-by-getattribute-by-servlet – Areej Aug 25 '17 at 13:42
  • you have written . write 'type' in place of onclick, then only that button will work as form submission button. Don't know why it is working sometime. – sanjiv saini Aug 25 '17 at 17:07
  • I changed it to type but still the problem happen sometimes – Areej Aug 27 '17 at 13:42

0 Answers0