0

Environment:

  • Wildfly 22
  • Java 11
  • JSF 2.3

I am trying to inject a ManagedProperty in a bean and I getting a NullPointerExcepion but I don't know exactly why is that. Something missing?

Error log

21:35:25,994 SEVERE [javax.enterprise.resource.webcontainer.jsf.application] (default task-8) Error Rendering View[/index.xhtml]: java.lang.NullPointerException
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.evaluateExpressionGet(ManagedPropertyProducer.java:87)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.lambda$new$0(ManagedPropertyProducer.java:60)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.CdiProducer.create(CdiProducer.java:105)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.create(ManagedPropertyProducer.java:38)
    at org.jboss.weld.core@3.1.5.Final//org.jboss.weld.contexts.unbound.DependentContextImpl.get(DependentContextImpl.java:64)
    ...

21:35:26,001 ERROR [io.undertow.request] (default task-8) UT005023: Exception handling request to /roscam/index.xhtml: javax.servlet.ServletException
    at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:725)
    at javax.faces.api@3.0.0.SP04//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at io.undertow.servlet@2.2.4.Final//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    ...
Caused by: java.lang.NullPointerException
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.evaluateExpressionGet(ManagedPropertyProducer.java:87)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.lambda$new$0(ManagedPropertyProducer.java:60)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.CdiProducer.create(CdiProducer.java:105)
    at com.sun.jsf-impl@2.3.14.SP02//com.sun.faces.cdi.ManagedPropertyProducer.create(ManagedPropertyProducer.java:38)
    ...
    ... 57 more

SessionBean

@Named
@SessionScoped
public class SessionBean implements Serializable {
    ...
    @Inject
    @ManagedProperty(value = "#{localeBean}")
    private LocaleBean localeB;//Error injecting bean NullPointerException
    ...
    @PostConstruct
    public void init() {
    ...

LocaleBean

@Named
@SessionScoped
public class LocaleBean implements Serializable {

   @PostConstruct
   public void init() {
...

index.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

<h:head>
</h:head>

<h:body>
    <f:view locale="#{localeBean.locale}">
       hi there
        #{sessionBean.doNothing}
    </f:view>
</h:body>

</html>

pom.xml

...
<dependencies>
    <dependency>
        <groupId>jakarta.platform</groupId>
        <artifactId>jakarta.jakartaee-api</artifactId>
        <version>8.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>10.0.0</version>
    </dependency>
    ...
Joe
  • 7,749
  • 19
  • 60
  • 110

3 Answers3

1

Isn't it possible to simply inject a LocaleBean without @ManagedProperty?

user1643352
  • 2,635
  • 2
  • 19
  • 25
  • I am migrating from former version of javaee and now ManagedProperty seems to be deprecated as appears in api https://jakarta.ee/specifications/platform/8/apidocs/javax/faces/bean/managedproperty – Joe May 15 '21 at 11:19
0

Runs without any problems with

  • Eclipse 4.18
  • AdoptJDK 11.0.5
  • WildFly 22.0.0.Final (not preview)
  • Mojarra 2.3.14.SP02

here

but remove the @ManagedProperty(value = "#{localeBean}") annotation.

So

package foo.bar;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;

@SessionScoped
@Named
public class LocaleBean implements Serializable {
    private static final long serialVersionUID = 7584442178216104053L;
    private String locale;

    public String getLocale() {
        return locale;
    }
}
package foo.bar;

import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Named
@SessionScoped
public class SessionBean implements Serializable {
    private static final long serialVersionUID = -6657315612830810889L;
    @Inject
    private LocaleBean localeB;
    private String doNothing = "foobar";

    public String getDoNothing() {
        return doNothing;
    }
}

works perfectly.

For this test you don't need the Maven stuff at all.

Try it without Primefaces in case of other errors.

Moreover it should be

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

If you get an

org.jboss.weld.exceptions.WeldException: WELD-001524: Unable to load proxy class for bean Managed Bean [class LocaleBean] with qualifiers [@Default @Any @Named] with class class LocaleBean
...
java.lang.ClassFormatError: Illegal class name "/LocaleBean$Proxy$_$$_WeldClientProxy" in class file /LocaleBean$Proxy$_$$_WeldClientProxy

error then you have to put your bean classes into a package: Weld creates invalid proxy for EJB classes in default package. Fixed in Weld 3.1.7.Final. WildFly 22.0.0.Final contains Weld 3.1.5.Final, WildFly 23.0.0.Final contains Weld 3.1.6.Final...

See also CDI Replacement for @ManagedProperty.

Toru
  • 905
  • 1
  • 9
  • 28
  • It looks that managedproperty has been deprecated as of jakartaee 8. I was using them i previous version so that's the cause of maven snippet with version I am migrating from former version of javaee and now ManagedProperty seems to be deprecated as appears in api https://jakarta.ee/specifications/platform/8/apidocs/javax/faces/bean/managedproperty – Joe May 15 '21 at 11:21
-1

As of jakartaee 8 ManagedProperty has been deprecated as we can see in its api.

So it is no loger needed to use @ManagedProperty with @Injecton.

Joe
  • 7,749
  • 19
  • 60
  • 110
  • 1
    This answer is incorrect. The code did not use the deprecated `javax.faces.bean.ManagedProperty`. Based on the stack trace in the question it actually used the `javax.faces.annotation.ManagedProperty` which is NOT deprecated. – BalusC May 16 '21 at 16:12
  • You are right! I assumed that was the problem because it worked by taking out @ManagedProperty. So still no clue why it throws NullPointerException... – Joe May 17 '21 at 06:46