title: JSF CDI ViewScoped Bean @PreDestroy throws Null Pointer Exception on Session Timeout
body: <p>I have CDI ViewScoped Bean with @Named and @ViewScoped Annotation. I have @PreConstruct and @PreDestroy functions defined. When I navigate from My JSF page to another page using h:commandLink, my @PreDestroy function gets called and all my resources gets freed up. But if the session times out, this @PreDestroy function call throws NullPointerException. I even tried having empty @PreDestroy function in the bean, but still it throws Null pointer Exception.
  What could be the possible reason this?</p>
  
  <pre><code>SEVERE: Exception thrown while destroying bean instance : [TestInfo, Name:testInfo, WebBeans Type:MANAGED, API Types:[java.io.Serializable,java.lang.Object,bean.TestInfo], Qualifiers:[javax.enterprise.inject.Default,javax.enterprise.inject.Any,javax.inject.Named]]
  java.lang.NullPointerException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          at java.lang.reflect.Method.invoke(Unknown Source)
          at  org.apache.webbeans.intercept.LifecycleInterceptorInvocationContext.proceed(LifecycleInterceptorInvocationContext.java:103)
      at org.apache.webbeans.portable.InjectionTargetImpl.preDestroy(InjectionTargetImpl.java:335)
      at org.apache.webbeans.component.AbstractOwbBean.destroy(AbstractOwbBean.java:180)
      at com.sun.faces.application.view.ViewScopeContextManager.destroyBeans(ViewScopeContextManager.java:145)
      at com.sun.faces.application.view.ViewScopeContextManager.sessionDestroyed(ViewScopeContextManager.java:275)
      at com.sun.faces.application.view.ViewScopeManager.sessionDestroyed(ViewScopeManager.java:345)
      at com.sun.faces.application.WebappLifecycleListener.sessionDestroyed(WebappLifecycleListener.java:177)
      at com.sun.faces.config.ConfigureListener.sessionDestroyed(ConfigureListener.java:368)
      at org.apache.catalina.session.StandardSession.expire(StandardSession.java:809)
      at org.apache.catalina.session.StandardSession.isValid(StandardSession.java:657)
      at org.apache.catalina.session.ManagerBase.processExpires(ManagerBase.java:532)
      at org.apache.catalina.session.ManagerBase.backgroundProcess(ManagerBase.java:517)
      at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1352)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1546)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1556)
      at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1524)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>