title: NullPointerException in REST service
body: <p>I am doing a REST service using Hibernate and Struts in <code>NetBeans</code> where I have created the <code>RESTful</code> service using Jersey <code>JAX-RS RI 2.5.1</code> and <code>JAX RS 2.0</code>  and I am having a error as follows:</p>
  <pre><code>type Exception report
  </code></pre>
  <p>message java.lang.NullPointerException</p>
  <p>description The server encountered an internal error that prevented it from fulfilling this request.</p>
  <pre><code>exception
  
  javax.servlet.ServletException: java.lang.NullPointerException
      org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:392)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
  
  root cause
  
  java.lang.NullPointerException
      com.myapp.struts.service.AbstractFacade.findAll(AbstractFacade.java:41)
      com.myapp.struts.service.UserTblFacadeREST.findAll(UserTblFacadeREST.java:66)
      sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      java.lang.reflect.Method.invoke(Method.java:498)
      org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
      org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:151)
      org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:171)
      org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:195)
      org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:104)
      org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:402)
      org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:349)
      org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:106)
      org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:259)
      org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      org.glassfish.jersey.internal.Errors.process(Errors.java:267)
      org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:318)
      org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
      org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1010)
      org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:382)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:345)
      org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:220)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
  </code></pre>
  <p>and My Code is:</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.myapp.struts.service;
  
  import com.myapp.struts.UserTbl;
  import java.util.List;
  import javax.persistence.EntityManager;
  import javax.persistence.PersistenceContext;
  import javax.ws.rs.Consumes;
  import javax.ws.rs.DELETE;
  import javax.ws.rs.GET;
  import javax.ws.rs.POST;
  import javax.ws.rs.PUT;
  import javax.ws.rs.Path;
  import javax.ws.rs.PathParam;
  import javax.ws.rs.Produces;
  
  /**
   *
   * @author Vihaan
   */
  @javax.ejb.Stateless
  @Path(&quot;usertbl&quot;)
  public class UserTblFacadeREST extends AbstractFacade&lt;UserTbl&gt; {
      @PersistenceContext(unitName = &quot;reststrutsPU&quot;)
      private EntityManager em;
  
      public UserTblFacadeREST() {
          super(UserTbl.class);
      }
  
      @POST
      @Override
      @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;})
      public void create(UserTbl entity) {
          super.create(entity);
      }
  
      @PUT
      @Path(&quot;{id}&quot;)
      @Consumes({&quot;application/xml&quot;, &quot;application/json&quot;})
      public void edit(@PathParam(&quot;id&quot;) Integer id, UserTbl entity) {
          super.edit(entity);
      }
  
      @DELETE
      @Path(&quot;{id}&quot;)
      public void remove(@PathParam(&quot;id&quot;) Integer id) {
          super.remove(super.find(id));
      }
  
      @GET
      @Path(&quot;{id}&quot;)
      @Produces({&quot;application/xml&quot;, &quot;application/json&quot;})
      public UserTbl find(@PathParam(&quot;id&quot;) Integer id) {
          return super.find(id);
      }
  
      @GET
      @Override
      @Produces({&quot;application/xml&quot;, &quot;application/json&quot;})
      public List&lt;UserTbl&gt; findAll() {
          return super.findAll();
      }
  
      @GET
      @Path(&quot;{from}/{to}&quot;)
      @Produces({&quot;application/xml&quot;, &quot;application/json&quot;})
      public List&lt;UserTbl&gt; findRange(@PathParam(&quot;from&quot;) Integer from, @PathParam(&quot;to&quot;) Integer to) {
          return super.findRange(new int[]{from, to});
      }
  
      @GET
      @Path(&quot;count&quot;)
      @Produces(&quot;text/plain&quot;)
      public String countREST() {
          return String.valueOf(super.count());
      }
  
      @Override
      protected EntityManager getEntityManager() {
          return em;
      }
      
  }
  </code></pre>
  <p>UserTbl.java</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.myapp.struts;
  
  import java.io.Serializable;
  import javax.persistence.Basic;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.Table;
  import javax.xml.bind.annotation.XmlRootElement;
  
  /**
   *
   * @author Vihaan
   */
  @Entity
  @Table(name = &quot;user_tbl&quot;)
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = &quot;UserTbl.findAll&quot;, query = &quot;SELECT u FROM UserTbl u&quot;),
      @NamedQuery(name = &quot;UserTbl.findById&quot;, query = &quot;SELECT u FROM UserTbl u WHERE u.id = :id&quot;),
      @NamedQuery(name = &quot;UserTbl.findByUsername&quot;, query = &quot;SELECT u FROM UserTbl u WHERE u.username = :username&quot;),
      @NamedQuery(name = &quot;UserTbl.findByPassword&quot;, query = &quot;SELECT u FROM UserTbl u WHERE u.password = :password&quot;),
      @NamedQuery(name = &quot;UserTbl.findByName&quot;, query = &quot;SELECT u FROM UserTbl u WHERE u.name = :name&quot;)})
  public class UserTbl implements Serializable {
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = &quot;ID&quot;)
      private Integer id;
      @Basic(optional = false)
      @Column(name = &quot;username&quot;)
      private String username;
      @Basic(optional = false)
      @Column(name = &quot;password&quot;)
      private String password;
      @Basic(optional = false)
      @Column(name = &quot;name&quot;)
      private String name;
  
      public UserTbl() {
      }
  
      public UserTbl(Integer id) {
          this.id = id;
      }
  
      public UserTbl(Integer id, String username, String password, String name) {
          this.id = id;
          this.username = username;
          this.password = password;
          this.name = name;
      }
  
      public Integer getId() {
          return id;
      }
  
      public void setId(Integer id) {
          this.id = id;
      }
  
      public String getUsername() {
          return username;
      }
  
      public void setUsername(String username) {
          this.username = username;
      }
  
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      @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 UserTbl)) {
              return false;
          }
          UserTbl other = (UserTbl) 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 &quot;com.myapp.struts.UserTbl[ id=&quot; + id + &quot; ]&quot;;
      }
      
  }
  </code></pre>
  <p>this is the AbstractFacade class</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.myapp.struts.service;
  
  import java.util.List;
  import javax.persistence.EntityManager;
  
  /**
   *
   * @author Vihaan
   */
  public abstract class AbstractFacade&lt;T&gt; {
      private Class&lt;T&gt; entityClass;
  
      public AbstractFacade(Class&lt;T&gt; entityClass) {
          this.entityClass = entityClass;
      }
  
      protected abstract EntityManager getEntityManager();
  
      public void create(T entity) {
          getEntityManager().persist(entity);
      }
  
      public void edit(T entity) {
          getEntityManager().merge(entity);
      }
  
      public void remove(T entity) {
          getEntityManager().remove(getEntityManager().merge(entity));
      }
  
      public T find(Object id) {
          return getEntityManager().find(entityClass, id);
      }
  
      public List&lt;T&gt; findAll() {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          cq.select(cq.from(entityClass));
          return getEntityManager().createQuery(cq).getResultList();
      }
  
      public List&lt;T&gt; findRange(int[] range) {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          cq.select(cq.from(entityClass));
          javax.persistence.Query q = getEntityManager().createQuery(cq);
          q.setMaxResults(range[1] - range[0] + 1);
          q.setFirstResult(range[0]);
          return q.getResultList();
      }
  
      public int count() {
          javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
          javax.persistence.criteria.Root&lt;T&gt; rt = cq.from(entityClass);
          cq.select(getEntityManager().getCriteriaBuilder().count(rt));
          javax.persistence.Query q = getEntityManager().createQuery(cq);
          return ((Long) q.getSingleResult()).intValue();
      }
      
  }
  </code></pre>