Joe
Joe

Reputation: 7919

NullPointerException on inject managedProperty on bean

Environment:

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 [email protected]//com.sun.faces.cdi.ManagedPropertyProducer.evaluateExpressionGet(ManagedPropertyProducer.java:87)
    at [email protected]//com.sun.faces.cdi.ManagedPropertyProducer.lambda$new$0(ManagedPropertyProducer.java:60)
    at [email protected]//com.sun.faces.cdi.CdiProducer.create(CdiProducer.java:105)
    at [email protected]//com.sun.faces.cdi.ManagedPropertyProducer.create(ManagedPropertyProducer.java:38)
    at [email protected]//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 [email protected]//javax.faces.webapp.FacesServlet.executeLifecyle(FacesServlet.java:725)
    at [email protected]//javax.faces.webapp.FacesServlet.service(FacesServlet.java:451)
    at [email protected]//io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
    ...
Caused by: java.lang.NullPointerException
    at [email protected]//com.sun.faces.cdi.ManagedPropertyProducer.evaluateExpressionGet(ManagedPropertyProducer.java:87)
    at [email protected]//com.sun.faces.cdi.ManagedPropertyProducer.lambda$new$0(ManagedPropertyProducer.java:60)
    at [email protected]//com.sun.faces.cdi.CdiProducer.create(CdiProducer.java:105)
    at [email protected]//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>
    ...

Upvotes: 0

Views: 589

Answers (3)

Joe
Joe

Reputation: 7919

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.

Upvotes: -1

Toru
Toru

Reputation: 905

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.

Upvotes: 0

user1643352
user1643352

Reputation: 2887

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

Upvotes: 1

Related Questions