RichFaces File Upload throws NullPointerException

吃可爱长大的小学妹 提交于 2019-12-24 09:53:22

问题


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]

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?


回答1:


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]

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();

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. 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();
    }
    
  2. Ensure that it is never null by instantiating it.

    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.



来源:https://stackoverflow.com/questions/4558733/richfaces-file-upload-throws-nullpointerexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!