环境:
- 野蝇 22
- 爪哇 11
- JSF 2.3
我试图在 bean 中注入一个 ManagedProperty 并且我得到一个 NullPointerExcepion 但我不知道为什么会这样。缺少什么?
错误日志
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
会话Bean
@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() {
...
索引.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>
...