Ilham Ilham -4 years ago 85
Ajax Question

Throws Error while Setting session value in servlet

I am sending json data from jsp to a servlet through ajax for store into the database. I am setting the session value in that servlet page to catch the session value at other jsp pages. but the server logs shows exception record.

This is the way i am setting the session :

HttpSession session = request.getSession(false);
session.setAttribute("userId", "123");


In server log:

11-Jun-2017 11:09:50.632 SEVERE [http-nio-80-exec-201] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [FBBackendServlet] in context with path [] threw exception
java.lang.NullPointerException
at Main.FBBackendServlet.processRequest(FBBackendServlet.java:236)
at Main.FBBackendServlet.doPost(FBBackendServlet.java:274)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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:744)

Answer Source

This code:

 HttpSession session = request.getSession(false);
 session.setAttribute("userId", "123");

is guaranteed to generate a NullPointerException on the second line if there is no pre-existing HttpSession object associated with the specific browser session that is returned in the request (typically via a Cookie named JSESSIONID.

In most cases you should just use request.getSession() so that the HttpSession object is created for you if necessary.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download