title: Login servlet gives error as null exception
body: <p>I have a login page and when I put the correct credentials in it gives <code>org.apache.jasper.JasperException: java.lang.NullPointerException</code> error.</p>
  
  <p>Here is the Authenticate.java (POJO class) which contains the methods to valid and read data from the database table:</p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.Exoticatravels;
  
  import java.util.Iterator;
  import java.util.List;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import javax.persistence.Query;
  
  /**
   *
   * @author sufiyaan
   */
  public class Authenticate {
  
      private String firstName;
      private String pass;
  
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("Exotica_TravelsPU");
      EntityManager em = emf.createEntityManager();
  
  
      public boolean findUser(String enteredEmail){
          Query query = em.createQuery("SELECT u FROM Users u WHERE u.email =" + "'" + enteredEmail + "'");
          List results = query.getResultList();  
  
          if(results.isEmpty()){
              return false;
          }
  
          Iterator it = results.iterator();
              while (it.hasNext()){
               Users user = (Users) it.next();
               firstName = user.getFirstName().toString();
               pass = user.getPassword().toString();
          }
              em.close();
              emf.close();
          return true;
  
      }
  
      public boolean authUser(String enteredPass){
  
          if(enteredPass.equals(pass)){
              return true;
          }
  
          return false;
      }
  
      public String getFirstName() {
          return firstName;
      }
  
      public String getPass() {
          return pass;
      }
  
  }
  </code></pre>
  
  <p>I am new to this and what I doubt is there is an error somewhere around here while assigning the value to the firstName variable.</p>
  
  <pre><code>user = (Users) it.next();
  firstName = user.getFirstName().toString();
  </code></pre>
  
  <p>Here is my Login Servlet:</p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.Exoticatravels;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Iterator;
  import java.util.List;
  import javax.persistence.EntityManager;
  import javax.persistence.EntityManagerFactory;
  import javax.persistence.Persistence;
  import javax.persistence.Query;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  /**
   *
   * @author sufiyaan
   */
  public class Login extends HttpServlet {
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
            String email = request.getParameter("email");
            String pass = request.getParameter("pass");
          Authenticate auth = new Authenticate();
  
          String firstName = auth.getFirstName();
  
          boolean isUser = auth.findUser(email);
          boolean isAuthenticated = auth.authUser(pass);
  
           if(isUser == false || isAuthenticated == false){
              RequestDispatcher rd = request.getRequestDispatcher("login.html");
              rd.forward(request, response);
          }
  
          HttpSession session = request.getSession();
          session.setAttribute("firstName", firstName);
  
          RequestDispatcher rd = request.getRequestDispatcher("welcome.jsp");
          rd.forward(request, response);
      }
  
  }
  </code></pre>
  
  <p>Here is the log for the program if it is helpful:</p>
  
  <pre><code>Warning:   StandardWrapperValve[Login]: Servlet.service() for servlet Login threw exception
  java.lang.NullPointerException
      at org.apache.jsp.welcome_jsp._jspService(welcome_jsp.java:80)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
      at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
      at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
      at com.Exoticatravels.Login.doPost(Login.java:51)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571)
      at java.lang.Thread.run(Thread.java:748)
  </code></pre>
  
  <p>Please help! Thanks in advance!</p>