首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NullPointerException在bean上注入managedProperty

NullPointerException在bean上注入managedProperty
EN

Stack Overflow用户
提问于 2021-05-14 20:09:34
回答 3查看 325关注 0票数 0

环境:

  • 野蝇22
  • Java 11
  • JSF 2.3

我试图在一个豆子中注入一个ManagedProperty,然后得到一个NullPointerExcepion,但我不知道确切的原因。少了点什么?

错误日志

代码语言:javascript
复制
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

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

LocaleBean

代码语言:javascript
复制
@Named
@SessionScoped
public class LocaleBean implements Serializable {

   @PostConstruct
   public void init() {
...

index.xhtml

代码语言:javascript
复制
<!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

代码语言:javascript
复制
...
<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>
    ...
EN

回答 3

Stack Overflow用户

发布于 2021-05-14 23:19:12

难道不可能在没有@ManagedProperty的情况下简单地注入LocaleBean吗?

票数 1
EN

Stack Overflow用户

发布于 2021-05-15 07:05:30

运行时没有任何问题。

  • 日食4.18
  • AdoptJDK 11.0.5
  • WildFly 22.0.Final(非预览)
  • Mojarra 2.3.14.SP 02

这里

但移除@ManagedProperty(value = "#{localeBean}") 注释

所以

代码语言:javascript
复制
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;
    }
}
代码语言:javascript
复制
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;
    }
}

效果很好。

对于这个测试,您根本不需要Maven的东西。

如果发生其他错误,请尝试不使用Primefaces。

而且它应该是

代码语言:javascript
复制
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://xmlns.jcp.org/jsf/html"
    xmlns:f="http://xmlns.jcp.org/jsf/core">

如果你得到一个

代码语言:javascript
复制
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

错误,则必须将bean类放入包:焊缝为默认包中的EJB类创建无效的代理。中。固定在焊接3.1.7最后。WildFly 22.0 WildFly包含焊缝3.1.5 WildFly,WildFly 23.0.0 WildFly包含焊缝3.1.6。

另见CDI替换@ManagedProperty

票数 0
EN

Stack Overflow用户

发布于 2021-05-15 11:24:12

正如我们在ManagedProperty的api接口中所看到的那样,jakartaee 8已经被废弃了。

因此,不需要在@Injecton中使用@ManagedProperty。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67540436

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档