Accessing Application Scoped Bean Causes NullPointerException

Posted by user2946861 on Stack Overflow See other posts from Stack Overflow or by user2946861
Published on 2013-11-03T02:24:02Z Indexed on 2013/11/03 3:53 UTC
Read the original article Hit count: 150

Filed under:

What is an Application Scoped Bean? I understand it to be a bean which will exist for the life of the application, but that doesn't appear to be the correct interpretation. I have an application which creates an application scoped bean on startup (eager=true) and then a session bean that tries to access the application scoped bean's objects (which are also application scoped). But when I try to access the application scoped bean from the session scoped bean, I get a null pointer exception. Here's excerpts from my code:

Application Scoped Bean:

    @ManagedBean(eager=true)
    @ApplicationScoped
    public class Bean1 implements Serializable{
      private static final long serialVersionUID = 12345L;
      protected ArrayList<App> apps;
      // construct apps so they are available for the session scoped bean
      // do time consuming stuff...
      // getters + setters

Session Scoped Bean:

   @ManagedBean
   @SessionScoped
   public class Bean2 implements Serializable{
     private static final long serialVersionUID = 123L;

     @Inject
     private Bean1 bean1;
     private ArrayList<App> apps = bean1.getApps();  // null pointer exception

What appears to be happening is, Bean1 is created, does it's stuff, then is destroyed before Bean2 can access it. I was hoping using application scoped would keep Bean1 around until the container was shutdown, or the application was killed, but this doesn't appear to be the case.

© Stack Overflow or respective owner

Related posts about jsf