title: HTTP Status 500 -with description -The server encountered an internal error that prevented it from fulfilling this request
body: <p>I am trying to implement listener program in which i get an HTTP status 500 Error once i submit with the index.html form page which redirect it to Servlet1 class.!</p>
  
  <p>Mylistener.java</p>
  
  <pre><code>import javax.servlet.annotation.WebListener;
  import javax.servlet.*;
  import javax.servlet.http.HttpSessionEvent;
  import javax.servlet.http.HttpSessionListener;
  
  
  @WebListener
  public class Mylistener implements HttpSessionListener {
  
    static int total=0, current=0;
    ServletContext ctx=null;
      public void sessionCreated(HttpSessionEvent e)  { 
          total++;
          current++;
         ctx=e.getSession().getServletContext();
         ctx.setAttribute("total users",total);
         ctx.setAttribute("looged users",current);
  
      }
  
      /**
       * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
       */
      public void sessionDestroyed(HttpSessionEvent e)  { 
           current--;  
           ctx.setAttribute("currentusers",current);  
      }
  
  }
  </code></pre>
  
  <p>![enter image description here][1</p>
  
  <p>Servlet1.java</p>
  
  <pre><code>import java.io.*;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.*;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  
  public class Servlet1 extends HttpServlet {
  
  
  
      protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
          res.setContentType("Text/html");
          PrintWriter out=res.getWriter();
          String n= req.getParameter("nname");
          System.out.println("Welcome"+n);
          HttpSession session=req.getSession();  
          session.setAttribute("uname",n);  
          ServletContext ctx= getServletContext();
          int i=(Integer)ctx.getAttribute("total");
          int c=(Integer)ctx.getAttribute("current");
          out.println("total users"+i);
          out.println("Current users"+c);
  
  
          out.close();
      }
  
  }
  </code></pre>
  
  <p>index.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset="ISO-8859-1"&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form action="servlet1" method="post"&gt;
  Enter your name &lt;input type="text" name="nname"&gt;&lt;br&gt;
  Enter your password : &lt;input type="password" name="npass"&gt;
  &lt;input type="submit" value="submit"&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <pre class="lang-html prettyprint-override"><code>web.xml
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" 
      xmlns="http://java.sun.com/xml/ns/javaee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
    &lt;listener&gt;
    &lt;listener-class&gt;Mylistener&lt;/listener-class&gt;
    &lt;/listener&gt;
  
    &lt;servlet&gt;
       &lt;servlet-name&gt;First&lt;/servlet-name&gt;
      &lt;servlet-class&gt;Servlet1&lt;/servlet-class&gt;
    &lt;/servlet&gt;
  
  
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;First&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/servlet1&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  
  
  &lt;/web-app&gt;
  </code></pre>
  
  
  
  <p>]</p>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>HTTP Status 500 - 
  
  
  type Exception report
  
  message 
  
  description The server encountered an internal error that prevented it from fulfilling this request.
  
  exception 
  java.lang.NullPointerException
      Servlet1.doPost(Servlet1.java:25)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
  </code></pre>
  
  <p>note The full stack trace of the root cause is available in the Apache Tomcat/7.0.59 logs.</p>