RichFaces文件上传引发NullPointerException [英] RichFaces File Upload throws NullPointerException

查看:56
本文介绍了RichFaces文件上传引发NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用RichFaces上传文件时遇到了很多麻烦(对于目前正在使用的许多技术,我是一个新手,这无疑使问题更加复杂了.)

I'm having a lot of trouble trying to get a file uploaded using RichFaces (I'm very new to a lot of the technologies I'm using at the moment which is definitely compounding the issue).

我可以在页面上添加文件上传组件,但是每当我尝试上传图片时,它始终会给我一个错误.

I'm able to add a the file upload component to the page but it keeps giving me an error whenever I try to upload an image.

我得到的错误如下(至少是它的开始):

The error I'm getting is as follows (at least the start of it is):

10:10:51,029 WARNING [javax.enterprise.resource.webcontainer.jsf.lifecycle] /profile.xhtml @49,25 fileUploadListener="#{editProfileAction.uploadListener}": java.lang.NullPointerException: javax.faces.el.EvaluationE
xception: /profile.xhtml @49,25 fileUploadListener="#{editProfileAction.uploadListener}": java.lang.NullPointerException
        at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73) [:1.1.15.B1]
        at org.richfaces.component.UIFileUpload.broadcast(UIFileUpload.java:190) [:3.3.3.CR1]
        at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:329) [:3.3.3.CR1]
        at org.ajax4jsf.component.AjaxViewRoot.broadcastEventsForPhase(AjaxViewRoot.java:302) [:3.3.3.CR1]
        at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:261) [:3.3.3.CR1]
        at org.ajax4jsf.component.AjaxViewRoot.processDecodes(AjaxViewRoot.java:417) [:3.3.3.CR1]

我不确定为什么会这样.我已经完成了Google搜索,但是没有找到任何遇到类似问题的人.

I'm unsure why this is happening. I've done a Google search and haven't had much luck in finding anyone with a similar problem.

有什么想法会导致这种情况吗?

Any ideas what could be causing this?

推荐答案

javax.faces.el.E​​valuationException:/profile.xhtml @ 49,25 fileUploadListener =#{editProfileAction.uploadListener}":java.lang.NullPointerException
在com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73)[:1.1.15.B1]

javax.faces.el.EvaluationException: /profile.xhtml @49,25 fileUploadListener="#{editProfileAction.uploadListener}": java.lang.NullPointerException
at com.sun.facelets.el.LegacyMethodBinding.invoke(LegacyMethodBinding.java:73) [:1.1.15.B1]

在第49行从字符25开始绑定到profile.xhtml中EL #{editProfileAction.uploadListener}的方法抛出了NullPointerException.您应该在堆栈跟踪中进一步查看详细信息,以"Caused by"或"Root cause"(在问题中省略)开头.该部分的第一行应包含在#{editProfileAction}受管bean后面的后备bean类的uploadListener()方法中引发NPE的确切行号.在代码中转到该行号,您将看到类似的内容:

The method which is bound to EL #{editProfileAction.uploadListener} in profile.xhtml, at line 49, starting at character 25, has thrown a NullPointerException. You should see the detail further in the stacktrace, starting with "Caused by" or "Root cause" (which you omitted in your question). The first line of that part should contain the exact line number where the NPE is been thrown in the uploadListener() method of the backing bean class behind #{editProfileAction} managed bean. Go to this line number in your code, you'll see something similiar as:

someObject.someMethod();

确切地说,使用点.运算符专注于字段/方法访问/调用.在这条线上的NPE仅表示使用点.运算符的对象参考是null.您不能在指向null的对象引用上使用点.运算符来访问字段或调用方法.它只会抛出一个NPE.基本上有两种方法可以解决此问题:

To be precise, concentrate on the field/method access/invocation using the dot . operator. A NPE on such a line simply means that the object reference on which the dot . operator is been used is null. You cannot access fields or invoke methods using the dot . operator on an object reference which points to null. It would only throw a NPE. There are basically two ways to fix this:

  1. 当引用为null时,跳过访问/调用.因此,只有在保证它不是null时才这样做.

  1. Skip access/invocation when the reference is null. So do it only when it is guaranteed to be not null.

if (someObject != null) {
    someObject.someMethod();
}

  • 通过实例化它永远不会null.

    if (someObject == null) {
        someObject = new SomeObject();
    }
    someObject.someMethod();
    

  • 哪种方法才是正确的解决方案,取决于唯一的功能要求和代码的上下文.

    Which way would be the right solution depends on the sole functional requirement and the context of the code.

    这篇关于RichFaces文件上传引发NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆