2014-02-10 57 views
0

我正嘗試創建一個超級簡單的Web應用程序,並在運行於Apache TomEE上的Java EE中進行日誌記錄。這樣做時,我遇到了一個javax.servlet.ServletException異常,該異常歸因於我認爲是一個愚蠢簡單的頁面的java.lang.NullPointerException異常。Java EE和JSF空指針異常

某些系統和圖書館信息:

- CentOS 6.5 
- Eclipse Kepler IDE for Java EE Developers 
- JRE System Library [java-1.7.0-openjdk-1.7.0.45.x86_64] 
- Apache Tomcat v7.0 
- Apache TomEE 1.6.0 JAX-RS 
- JSF Mojara 2.2 (javax.faces.jar) 
- log4j 1.2.17 (log4j-1.2.17.jar) 

太簡單頁面(home.xhtml):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

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

    <h:head> 
     <title>LogTest Home</title> 
    </h:head> 

    <h:body> 
     <h1>Testing:</h1> 
     <h:outputText value="#{logTest.foo()}"/> 
    </h:body> 
</html> 

的支持bean(logTest.java):

package test.pack; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

import org.apache.log4j.Logger; 

@ManagedBean(name="logTest") 
@SessionScoped 
public class logTest { 

    private static final Logger LOGGER = Logger.getLogger(logTest.class); 

    private String statusMsg = "Default status"; 

    public String getStatusMsg() { 
     return statusMsg; 
    } 

    public void setStatusMsg(String statusMsg) { 
     this.statusMsg = statusMsg; 
    } 

    public String foo() { 
     LOGGER.info("Currently executing method foo()"); 
     setStatusmsg("Hello foo()!"); 
     return "foo() executed successfully!"; 
    } 
} 

的log4j .properties文件位於/ WEB-INF/classes目錄中

log4j.rootLogger = TRACE, toConsole, toFile 

#Test File Logs 
log4j.appender.toFile=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.toFile=/opt/tommee/logs/log4jWebapp.log 
log4j.appender.toFile.DatePattern='.'yyyy-MM-dd 
log4j.appender.toFile.layout=org.apache.log4j.PatternLayout 
log4j.appender.toFile.layout.ConversionPattern=%d %5p [%t] - %c%M - %m%n 

#Test Console Logs 
log4j.appender.toConsole=org.apache.log4j.ConsoleAppender 
log4j.appender.toConsole.layout=org.apache.log4j.PatternLayout 
log4j.appender.toConsole.layout.ConversionPattern=%d %5p [%t] - %c%M - %m%n 

部署描述符(web.xml)

<?xml version"1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://wwww.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
     http://java.sun.com/xml/ns/javaaee/web-app_3_0.xsd" 
    version="3.0"> 

    <display-name>log4jTEst</display-name> 

    <welcome-file-list> 
     <welcome-filee>faces/home.xhtml</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.faces</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
     <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
     <param-value>client</param-value> 
    </context-param> 
    <context-param> 
     <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
     <param-value>resources.application</param-value> 
    </context-param> 

    <listener> 
     <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
    </listener> 

</web-app> 

現在,當我我已經建立了在服務器上運行,我得到的回報,這從Eclipse瀏覽器:

HTTP Status 500- 

type - Exception report 

message 

description - The server encountered an internal error that prevented it from fulfilling this request. 

exception - 

javax.servlet.ServletException 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659) 
    org.apachee.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 

root cause - 

java.lang.NullPointerException 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    org.apachee.el.parser.AstValue.getValue(AstValue.java:173) 
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.jaava:109) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) 
    javax.faces.component.UIOutput.getValue(UIOutput.java:174) 
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) 
    javax.faces.component.UIComponentBase.encodeEnd(UIComponent.java:924) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1863) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443) 
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 

note - the full stack trace of the root cause is available in the Apache Tomcat (TomEE)/7.00.47 (1.6.0) logs 

這裏的服務器在啓動後立即登錄:

Feb 10, 2014 4:40:29 PM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException 
SEVERE: Error Rendering View[/home.xhtml] 
java.lang.NullPointerException 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    org.apachee.el.parser.AstValue.getValue(AstValue.java:173) 
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.jaava:109) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) 
    javax.faces.component.UIOutput.getValue(UIOutput.java:174) 
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) 
    javax.faces.component.UIComponentBase.encodeEnd(UIComponent.java:924) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1863) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443) 
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    org.apache.catalina.core.StandardContextValve.invoke(StadnardContextValve.java:123) 
    org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    java.lang.Thread.run(Thread.java:744) 

Feb 10, 2014 4:40:29 PM org.apache.catalina.core.StandaardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [Faces Servlet] in context with hpath [/log4jTest] threw exception [null] with root cause 
java.lang.NullPointerExpression 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    javax.el.CompositeELResolver.invoke(CompositeELResolver.java:255) 
    org.apachee.el.parser.AstValue.getValue(AstValue.java:172) 
    org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:185) 
    com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.jaava:109) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194) 
    javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182) 
    javax.faces.component.UIOutput.getValue(UIOutput.java:174) 
    com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.getCurrentValue(HtmlBasicRenderer.java:355) 
    com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeEnd(HtmlBasicRenderer.java:164) 
    javax.faces.component.UIComponentBase.encodeEnd(UIComponent.java:924) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1863) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    javax.faces.component.UIComponent.encodeALL(UIComponent.java:1859) 
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:443) 
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131) 
    javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337) 
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120) 
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219) 
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:647) 
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    org.apache.catalina.core.StandardContextValve.invoke(StadnardContextValve.java:123) 
    org.apache.tomee.catalina.OpenEJBValve.invoke(OpenEJBValve.java:45) 
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) 
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) 
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) 
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    java.lang.Thread.run(Thread.java:744) 

所以,我需要幾件事情: A-解決這個nullPointerException(我相信它來自我自己對Java EE缺乏瞭解,因此有些濫用平臺。 B-有關我如何正確或不正確使用Java EE的信息以及可幫助我改進的信息/資源。主要修正這個簡單的應用程序。

對我來說,在寫下這個幫助請求時,我偶然發現了關於設置日誌記錄的問題的答案。很明顯,我現在就開始工作了,這很好,但是上面的問題通過我的調整持續下去。

感謝您的閱讀,我欣賞任何建議,更正和支持!

+0

修復方法'公共字符串setStatusMsg(){this.statusMsg = statusMsg; }'=>它應該是'public void setStatusMsg(String statusMsg){this.statusMsg = statusMsg; }' –

+0

我在文章中解決了這個問題,但這就是我在代碼中所做的。我錯了,讓錯過了,抱歉。 – MarekVarro

+0

TomEE附帶MyFaces,但您通過webapp發送Mojarra。這些將與「怪異」的例外相沖突。在webapp中刪除'javax.faces.jar'(僅當您部署到沒有捆綁JSF的服務器(例如Tomcat)時才需要),依賴於TomEE自己的MyFaces並重試。 – BalusC

回答

0

所以,當您的網頁上有<h:outputText value="#{logTest.foo()}"/>,什麼樣的處理器讀取是:

  1. 有可能是一個在託管bean稱爲foo變量
  2. 將有一個在管理一個名爲getFoo()方法bean,按照JavaBean方法命名標準。

您的班級在兩次計數都失敗,因此出現錯誤。


<h:outputText/>期待值綁定即用於在類實際的屬性,而不是隻是返回一個值(不考慮參數)的方法的吸氣劑。那麼,什麼是預期的是:

public class logTest { 

    private static final Logger LOGGER = Logger.getLogger(logTest.class); 

    private String statusMsg = "Default status"; 

    private String foo = "TheFoo" //There should be a named variable 

     public String getStatusMsg() { 
     return statusMsg; 
     } 

     public void setStatusMsg(String statusMsg) { 
     this.statusMsg = statusMsg; 
     } 

     public String getFoo(){ 
     LOGGER.info("Currently executing method getFoo()"); 
     return this.foo; 
     } 

     public String foo() { 
     LOGGER.info("Currently executing method foo()"); 
     setStatusmsg("Hello foo()!"); 
     return "foo() executed successfully!"; 
     } 
} 

欲瞭解更多信息:

+0

雖然這是不好的做法,但實際上您應該可以在EL-2.2及更高版本中調用值表達式的方法,請參見[BalusC的答案](http://stackoverflow.com/a/10202001/785663)。不過,我無法解釋NPE。也許'logTest'類本身沒有被實例化? – mabi