JSF 空指针异常

发布于 2025-01-04 17:36:54 字数 3314 浏览 0 评论 0原文

我在使用网络应用程序时遇到空指针异常。 该异常在登录页面抛出。我输入了用户名和密码,但表达式 #{userBean.user}#{userBean.pass} 的值或求值似乎返回 null。

我的配置:Apache Tomcat 7.0.22、Mojarra 2.1.6、RichFaces 4.1

这是堆栈跟踪

javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1824)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at mx.gob.sagarpa.utilidades.Database.checkUser(Database.java:61)
    at mx.gob.sagarpa.beans.UserBean.doLogin(UserBean.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

我能做什么?

I'm getting a Null Pointer Exception when using my web app.
This exception is thrown at the login page. I enter both username and pass but it seems that the value or the evaluation of the expression #{userBean.user} or #{userBean.pass} is returning null.

My configuration: Apache Tomcat 7.0.22, Mojarra 2.1.6, RichFaces 4.1

Here's the stacktrace

javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1824)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.NullPointerException
    at mx.gob.sagarpa.utilidades.Database.checkUser(Database.java:61)
    at mx.gob.sagarpa.beans.UserBean.doLogin(UserBean.java:70)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 24 more

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2025-01-11 17:36:54

您需要前往堆栈跟踪最底部的“Caused by”部分来查找真正的根本原因。在您的特定情况下,如下所示:

Caused by: java.lang.NullPointerException
    at mx.gob.sagarpa.utilidades.Database.checkUser(Database.java:61)

您会看到,在 Database 类的第 61 行,在 checkUser() 方法内,某些对象引用为 null,而代码通过句点运算符 . 访问它。

someObject = null;
someObject.doSomething(); // NullPointerException!

要修复它,您需要添加 nullcheck

if (someObject != null) {
    someObject.doSomething();
} else {
    // Take alternate path?
}

或确保它永远不会 null

someObject = createItSomehow();
someObject.doSomething(); // Okay.

采用哪条路径取决于问题中不清楚的具体功能要求。但您现在至少应该了解为什么会发生 NullPointerException 。这也在其 javadoc 中进行了解释(其中应该是查找类信息的第一来源,包括异常):

当应用程序在需要对象的情况下尝试使用 null 时抛出。其中包括:

  • 调用 null 对象的实例方法。
  • 访问或修改 null 对象的字段。
  • null 的长度视为数组。
  • 像数组一样访问或修改 null 的槽。
  • 抛出 null,就好像它是一个 Throwable 值。

应用程序应该抛出此类的实例来指示 null 对象的其他非法使用。

也就是说,这个问题与 JSF 并不完全相关。这只是基本的Java。我建议花一些时间阅读一本像样的基础 Java 书籍/教程。

You need to head to the bottomost "Caused by" part of the stacktrace for the real root cause. In your particular case that is the following:

Caused by: java.lang.NullPointerException
    at mx.gob.sagarpa.utilidades.Database.checkUser(Database.java:61)

You see, at line 61 of Database class, inside the checkUser() method, some object reference is null while the code is accessing it by the period operator ..

someObject = null;
someObject.doSomething(); // NullPointerException!

To fix it, you'd need to either add a nullcheck

if (someObject != null) {
    someObject.doSomething();
} else {
    // Take alternate path?
}

or to make sure that it's never null

someObject = createItSomehow();
someObject.doSomething(); // Okay.

Which path to take depends on the concrete functional requirement which is not clear from the question. But you should now at least understand why a NullPointerException can occur. This is also explained in its javadoc (which should be the #1 source for finding information about classes, including exceptions):

Thrown when an application attempts to use null in a case where an object is required. These include:

  • Calling the instance method of a null object.
  • Accessing or modifying the field of a null object.
  • Taking the length of null as if it were an array.
  • Accessing or modifying the slots of null as if it were an array.
  • Throwing null as if it were a Throwable value.

Applications should throw instances of this class to indicate other illegal uses of the null object.

That said, this problem is not exactly related to JSF. It's just basic Java. I'd suggest to take some time to go through a decent basic Java book/tutorial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文