Question

I was using OmniFaces #{now} with PrimeFaces 3.5 and JSF 2.1 to obtain current date in EL. It has always worked fine, but after recent migration to PrimeFaces 4.0 and JSF 2.2, using #{now} throws a NullPointerException.

Caused by: java.lang.NullPointerException
    at org.glassfish.weld.DeploymentImpl.findRootBda(DeploymentImpl.java:700)
    at org.glassfish.weld.DeploymentImpl.getBeanDeploymentArchive(DeploymentImpl.java:682)
    at org.jboss.weld.manager.BeanManagerLookupService.lookupBeanManager(BeanManagerLookupService.java:48)
    at org.jboss.weld.manager.BeanManagerLookupService.lookupBeanManager(BeanManagerLookupService.java:60)
    at org.jboss.weld.manager.BeanManagerImpl.getInjectionTargetFactory(BeanManagerImpl.java:1381)
    at org.jboss.weld.manager.BeanManagerImpl.createInjectionTarget(BeanManagerImpl.java:1039)
    at org.glassfish.weld.services.JCDIServiceImpl.injectManagedObject(JCDIServiceImpl.java:283)
    at org.glassfish.faces.integration.GlassFishInjectionProvider.inject(GlassFishInjectionProvider.java:189)
    at com.sun.faces.mgbean.BeanBuilder.injectResources(BeanBuilder.java:203)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:101)
    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)
    at com.sun.el.parser.AstIdentifier.getValue(AstIdentifier.java:116)
    at com.sun.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:226)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:150)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
    ... 39 more

How is this caused and how can I solve it?

Was it helpful?

Solution

This is caused by a bug in GlassFish 4.0 known as issue 20775.

You have basically 3 options:

  1. Use a different server. Since GlassFish 4.0, it's officially announced that the open source version is not recommended for production. Oracle stopped commercial support on it. As of now, it's still exactly the same as the first release at July 2013, full of various childhood bugs. Currently, your only open source alternative is JBoss WildFly. Other vendors don't have a production ready Java EE 7 application server yet.


  2. Create a class which extends java.util.Date:

    public class Now extends Date {
        private static final long serialVersionUID = 1L;
    }
    

    And explicitly register it as managed bean in webapp's own /WEB-INF/faces-config.xml, so that the OmniFaces one gets overridden:

    <managed-bean>
        <managed-bean-name>now</managed-bean-name>
        <managed-bean-class>com.example.Now</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
    

  3. Manually set #{now} using a servlet filter. At its simplest, it can look like this in its entirety (imports omitted for brevity):

    @WebFilter("/*")
    public class ApplicationFilter extends HttpFilter {
    
        @Override
        public void doFilter(HttpServletRequest request, HttpServletResponse response, HttpSession session, FilterChain chain) throws ServletException, IOException {
            request.setAttribute("now", new Date());
            chain.doFilter(request, response);
        }
    
    }
    

Update: this awkward bug has been fixed in GlassFish 4.1, apparently as consequence of another bugfix. So if you still want to stick to GlassFish (despite it not being recommended for production) and if you can upgrade, then upgrade the old fish as soon as possible. It has many, many, more bugfixes in at least the JSF and CDI areas, so it's definitely worth the effort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top