简体   繁体   中英

RichFaces File Upload throws NullPointerException

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM