title: NullPointerException while trying to access @EJB bean in managed bean constructor
body: <p>I've an EJB service.</p>
  
  <pre><code>@Stateless
  public class SomeService {}
  </code></pre>
  
  <p>I'd like to inject this in a viewscoped bean and initialize with it:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class ViewBean implements Serializable {
  
      @EJB
      private SomeService someService;
  
      public ViewBean() {
          System.out.println(someService.getEntity());
      }
  
  }
  </code></pre>
  
  <p>However, it throws the following exception:</p>
  
  <pre><code>com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.example.ViewBean.
      at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:193)
      at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:102)
      at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
      at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
      at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
      at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
      at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
      at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
      [snip]
  Caused by: java.lang.NullPointerException
      at com.example.ViewBean.&lt;init&gt;(ViewBean.java:42)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
      at java.lang.Class.newInstance0(Class.java:374)
      at java.lang.Class.newInstance(Class.java:327)
      at com.sun.faces.mgbean.BeanBuilder.newBeanInstance(BeanBuilder.java:188)
      ... 62 more
  </code></pre>
  
  <p>How is this caused and how can I solve it?</p>