title: Servlet.service() for servlet jsp threw exception java.lang.NullPointerException while calling EJB bean
body: <p>While calling EJB bean on my simple jsp page I have such an error:</p>
  
  <pre><code> WARNING: StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw 
   exception java.lang.NullPointerException
  at appjpa.ejb.UsersFacade.findAllStudents(UsersFacade.java:65)
  at org.apache.jsp.Students_jsp._jspService(Students_jsp.java from :75)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:403)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:492)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:378)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
  at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>Code for my jsp page is such:</p>
  
  <pre><code>&lt;%@page import="appjpa.entities.Users"%&gt;
  &lt;%@page import="java.util.ArrayList"%&gt;
  &lt;%@page import="socnet2.Student"%&gt; &lt;!-- class Student emulates real student --&gt;
  &lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
   &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
      &lt;title&gt;JSP Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;jsp:useBean class="appjpa.ejb.UsersFacade" id="dao" scope="application"&gt;&lt;/jsp:useBean&gt;
      &lt;p&gt;&lt;b&gt;List of all students:&lt;/b&gt;&lt;/p&gt;
      &lt;form action="/AppJPA/Edit" method="POST"&gt;
         &lt;jsp:scriptlet&gt;
         int counter = 0;  if (dao != null){
         for (Users s : dao.findAllStudents()) {
          &lt;/jsp:scriptlet&gt;
          &lt;p&gt; &lt;jsp:expression&gt; s.getName() + " " + s.getSurname()&lt;/jsp:expression&gt;&lt;/p&gt;
          &lt;input type="submit" name="Edit_&lt;%=counter%&gt;" value="Edit" /&gt;
          &lt;input type="submit" name="Delete_&lt;%=counter%&gt;" value ="Delete"/&gt; 
           &lt;jsp:scriptlet&gt;
                  counter++;
              }}
          &lt;/jsp:scriptlet&gt;
          &lt;input type="submit" name="Add_new_student" value ="Add new student"/&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p></p>
  
  <p>Autogenerated by NetBeans 7.0.1 wizard entity class for users looks like:</p>
  
  <pre><code> @Entity
  @Table(name = "USERS")
  @NamedQueries({
  @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
  @NamedQuery(name = "Users.findById", query = "SELECT u FROM Users u WHERE u.id = :id"),
  @NamedQuery(name = "Users.findByName", query = "SELECT u FROM Users u WHERE u.name =      :name"),
  @NamedQuery(name = "Users.findBySurname", query = "SELECT u FROM Users u WHERE u.surname = :surname"),
  @NamedQuery(name = "Users.findByLogin", query = "SELECT u FROM Users u WHERE u.login = :login"),
  @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password"),
  @NamedQuery(name = "Users.findByRole", query = "SELECT u FROM Users u WHERE u.role = :role")})
  public class Users implements Serializable {
  private static final long serialVersionUID = 1L;
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  // @NotNull
  @Column(name = "ID")
  private Integer id;
  @Size(max = 40)
  @Column(name = "NAME")
  private String name;
  @Size(max = 40)
  @Column(name = "SURNAME")
  private String surname;
  @Size(max = 40)
  @Column(name = "LOGIN")
  private String login;
  @Size(max = 40)
  @Column(name = "PASSWORD")
  private String password;
  @Size(max = 40)
  @Column(name = "ROLE")
  private String role;
  @ManyToMany(mappedBy = "usersList")
  private List&lt;SubjectGroup&gt; subjectGroupList;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "idCreator")
  private List&lt;SubjectGroup&gt; subjectGroupList1;
  @OneToMany(cascade = CascadeType.ALL, mappedBy = "idSender")
  private List&lt;Messages&gt; messagesList;
  
  public Users() {
  }
  
  public Users(Integer id) {
      this.id = id;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getSurname() {
      return surname;
  }
  
  public void setSurname(String surname) {
      this.surname = surname;
  }
  
  public String getLogin() {
      return login;
  }
  
  public void setLogin(String login) {
      this.login = login;
  }
  
  public String getPassword() {
      return password;
  }
  
  public void setPassword(String password) {
      this.password = password;
  }
  
  public String getRole() {
      return role;
  }
  
  public void setRole(String role) {
      this.role = role;
  }
  
  public List&lt;SubjectGroup&gt; getSubjectGroupList() {
      return subjectGroupList;
  }
  
  public void setSubjectGroupList(List&lt;SubjectGroup&gt; subjectGroupList) {
      this.subjectGroupList = subjectGroupList;
  }
  
  public List&lt;SubjectGroup&gt; getSubjectGroupList1() {
      return subjectGroupList1;
  }
  
  public void setSubjectGroupList1(List&lt;SubjectGroup&gt; subjectGroupList1) {
      this.subjectGroupList1 = subjectGroupList1;
  }
  
  public List&lt;Messages&gt; getMessagesList() {
      return messagesList;
  }
  
  public void setMessagesList(List&lt;Messages&gt; messagesList) {
      this.messagesList = messagesList;
  }
  
  @Override
  public int hashCode() {
      int hash = 0;
      hash += (id != null ? id.hashCode() : 0);
      return hash;
  }
  
  @Override
  public boolean equals(Object object) {
      // TODO: Warning - this method won't work in the case the id fields are not set
      if (!(object instanceof Users)) {
          return false;
      }
      Users other = (Users) object;
      if ((this.id == null &amp;&amp; other.id != null) || (this.id != null &amp;&amp; !this.id.equals(other.id))) {
          return false;
      }
      return true;
  }
  
  @Override
  public String toString() {
      return "appjpa.entities.Users[ id=" + id + " ]";
  }
  
  }
  </code></pre>
  
  <p>And, finaly, class UsersFacade is such:</p>
  
  <pre><code>@Stateless
  public class UsersFacade extends AbstractFacade&lt;Users&gt; implements UsersFacadeLocal {
  @PersistenceContext(unitName = "AppJPAPU")
  private EntityManager em;
  
  protected EntityManager getEntityManager() {
      return em;
  }
  
  public UsersFacade() {
      super(Users.class);
  }
  
  @Override
  public Users checkInBase(String login, String password) {
      Users u = (Users)(em.createNamedQuery("Users.findByLogin").setParameter("login",      login).getSingleResult());
  
      return u;
  }
  
  @Override
  public void removeUserFromSubjectGroup(Users user, SubjectGroup group) {
      Users u = em.find(Users.class, user.getId());
      SubjectGroup g = em.find(SubjectGroup.class, group.getId());
      if (u != null &amp;&amp; g != null) {
          user.getSubjectGroupList().remove((SubjectGroup)group);
          group.getUsersList().remove(user);
          em.merge(user);
          em.merge(group);
      }
  }
  
  @Override
  public void addUserToSubjectGroup(Users user, SubjectGroup group) {
      Users u = em.find(Users.class, user.getId());
      SubjectGroup g = em.find(SubjectGroup.class, group.getId());
      if (u != null &amp;&amp; g != null) {
          user.getSubjectGroupList().add((SubjectGroup)group);
          group.getUsersList().add(user);
          em.merge(user);
          em.merge(group);
      }
  }
  
  @Override
  public List&lt;Users&gt; findAllStudents() {
      String role = new String("student");
       List&lt;Users&gt; students = (List&lt;Users&gt;)(em.createNamedQuery("SELECT u FROM Users u WHERE u.role = :role").setParameter("role", role).getResultList());
       if (students == null){
           System.out.println("Students are null!!!");
       }
  
      return students;
  }
  
  @Override
  public boolean deleteStudent(Users user) {
      Users u = em.find(Users.class, user.getId());
      em.remove(user);
      return true;
  }
  
  @Override
  public boolean updateStudent(Users studentToEdit, String newName, String newSurname, String newLogin, String newPassword) {
      Users u = em.find(Users.class, studentToEdit.getId());
      u.setName(newName);
      u.setSurname(newSurname);
      u.setLogin(newLogin);
      u.setPassword(newPassword);
       em.merge(studentToEdit);
      return true;
  }
  
  @Override
  public void insertStudenttoDB(Users student, String newName, String newSurname, String newLogin, String newPassword) {
     em.persist(student);
     Users u = em.find(Users.class, student.getId());
      if (u != null) {
      student.setName(newName);
      student.setSurname(newSurname);
      student.setLogin(newLogin);
      student.setPassword(newPassword);  
      em.merge(student);
      }
  }     
  }
  </code></pre>
  
  <p>I am quite new to EJB and maybe I am not using them right. I tried calling other methods from UsersFaced at another pages, but received the same error. Please, explain what I am doing wrong...</p>