10 Replies Latest reply on Apr 5, 2007 9:20 AM by ghiormar

    Help on s:filleupload

    xigua

      Hi,

      I am trying to use the fileupload component provided by Seam 1.1.6 and it worked if I actually select a file. The issue is that, if I didn't select any file, then I am getting NPE:

      2007-03-08 14:26:40,987 ERROR org.jboss.seam.servlet.SeamExceptionFilter [] - exception root cause
      java.lang.NullPointerException
       at java.io.FileInputStream.<init>(FileInputStream.java:103)
       at org.jboss.seam.servlet.MultipartRequest$FileParam.getInputStream(MultipartRequest.java:220)
       at org.jboss.seam.servlet.MultipartRequest.getFileInputStream(MultipartRequest.java:479)
       at org.jboss.seam.ui.FileUpload.decode(FileUpload.java:54)
       at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:606)
       at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:602)
       at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:602)
       at javax.faces.component.UIForm.processDecodes(UIForm.java:53)
       at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:602)
       at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:135)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot.access$001(AjaxViewRoot.java:46)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot$1.invokeRoot(AjaxViewRoot.java:247)
       at org.ajax4jsf.framework.ajax.JsfOneOneInvoker.invokeOnRegionOrRoot(JsfOneOneInvoker.java:36)
       at org.ajax4jsf.framework.ajax.AjaxContext.invokeOnRegionOrRoot(AjaxContext.java:168)
       at org.ajax4jsf.framework.ajax.AjaxViewRoot.processDecodes(AjaxViewRoot.java:260)
       at org.apache.myfaces.lifecycle.LifecycleImpl.applyRequestValues(LifecycleImpl.java:219)
       at org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:71)
      


      I am using InputStream in the managed-bean.

      Is there any way to make it work even if I didn't select any file?

      Thanks,
      xigua

        • 1. Re: Help on s:filleupload
          stu2

          It looks like you're reading in the file even if one wasn't selected for uploading. In my file upload bean I check to see if a file was actually uploaded. If not, don't read it in. Here's how it looks for me:


           if (uploadFile == null) {
           error("Hmm. uploaded file data is null!");
           return;
           }
          


          Could this be your problem?

          • 2. Re: Help on s:filleupload
            xigua

             

            "stu2" wrote:
            It looks like you're reading in the file even if one wasn't selected for uploading. In my file upload bean I check to see if a file was actually uploaded. If not, don't read it in. Here's how it looks for me:


             if (uploadFile == null) {
             error("Hmm. uploaded file data is null!");
             return;
             }
            


            Could this be your problem?


            I don't think so since the NPE happens in org.jboss.seam.ui.FileUpload.decode
            Maybe I misunderstood.

            So I have a backing bean which has a InputSteam field and there are getter and setter for it. So on the front end, I use <s:fileUpload data="#{backingbean.filedata}" />. My understanding is that if user select the file, then the file will be uploaded and set to the InputStream field otherwise set it to null, I didn't try to read the file at all. It seems to me that once I use fileUpload component and bind it to a field of the backing bean, the seam component will try to read the file no matter whether there is file selected?

            • 3. Re: Help on s:filleupload
              stu2

              Ahh, we're using it differently. I'm following the seam examples, where the upload componet sets the value to a bean variable that is a byte []. For instance this is from my backing bean (with the obvious getters/setters left out):

               private String uploadFileName;
               private byte [] uploadFile;
              


              and here's what the facelets page looks like:

              <s:fileUpload id="feed" data="#{feedMapping.uploadFile}" fileName="#{feedMapping.uploadFileName}"/>
              


              That plus the null check from my previous post works well for me, and I don't get the nullpointer you're seeing.

              Does that help?

              • 4. Re: Help on s:filleupload
                shane.bryzak

                The NPE is fixed in Seam 1.1.7 and higher. I recommend you update to the latest release.

                • 5. Re: Help on s:filleupload
                  xigua

                   

                  "shane.bryzak@jboss.com" wrote:
                  The NPE is fixed in Seam 1.1.7 and higher. I recommend you update to the latest release.


                  Thanks a lot. And I confirmed that NPE only exists if I use InputStream instead of byte[].

                  • 6. Re: Help on s:filleupload
                    carloszaniolo

                    BTW s:fileupload is not working together with Ajax4jsf! Does anyone knows how to make s:fileupload works in an "Ajax" way?

                    • 7. Re: Help on s:filleupload
                      carloszaniolo

                       

                      "carloszaniolo" wrote:
                      BTW s:fileupload is not working together with Ajax4jsf! Does anyone knows how to make s:fileupload works in an "Ajax" way?


                      http://jboss.com/index.html?module=bb&op=viewtopic&p=4026758#4026758

                      • 8. Re: Help on s:filleupload

                        Is it normal behavior for the <s:fileUpload> to call the setter for byte[] twice? I'm mean first it calls it for the byte[] with the file data and then it calls it with a null. I used seam 1.2.1.GA on jboss 4.0.5.GA. The project was build with seam-gen.

                        package app.beans.entity;
                        
                        import javax.ejb.Remove;
                        import javax.ejb.Stateful;
                        
                        import org.jboss.seam.ScopeType;
                        import org.jboss.seam.annotations.Destroy;
                        import org.jboss.seam.annotations.Logger;
                        import org.jboss.seam.annotations.Name;
                        import org.jboss.seam.annotations.Scope;
                        import org.jboss.seam.core.FacesMessages;
                        import org.jboss.seam.log.Log;
                        
                        @Stateful
                        @Name("uploadFile")
                        @Scope(ScopeType.EVENT)
                        public class UploadFile implements java.io.Serializable, UploadFileI {
                         private static final long serialVersionUID = 940956938635067807L;
                         @Logger
                         private Log log;
                         private byte[] byteArray;
                         private String fileName;
                         private String size;
                         private static int count = 0;
                         public UploadFile() {
                         }
                         public String getFileName() {
                         return fileName;
                         }
                         public void setFileName(String fileName) {
                         this.fileName = fileName;
                         FacesMessages.instance().add("i'm here set File name = "+this.fileName);
                         }
                         public byte[] getByteArray() {
                         return byteArray;
                         }
                         public void setByteArray(byte[] byteArray) {
                         count++;
                        //SUGESTED FIX
                        // if (byteArray == null) {
                        // FacesMessages.instance().add("i'm here setByteArray("+byteArray+");null count:"+count);
                        // return;
                        // }
                         this.byteArray = byteArray;
                         FacesMessages.instance().add("i'm here setByteArray("+byteArray+"); count:"+count);
                         }
                         public String getSize() {return size;}
                         public void setSize(String size) {
                        this.size = size;
                         FacesMessages.instance().add("i'm here set File size = "+this.size);
                         }
                        
                         @Remove @Destroy
                         public void destroy() {
                         }
                         public void reset() {
                         count=0;
                         }
                        }

                        the interface
                        package app.beans.entity;
                        
                        import org.jboss.annotation.ejb.Local;
                        
                        @Local
                        public interface UploadFileI {
                         public String getFileName();
                         public void setFileName(String fileName);
                         public byte[] getByteArray();
                         public void setByteArray(byte[] byteArray);
                         public String getSize();
                         public void setSize(String size);
                         public void destroy();
                         public void reset();
                        }
                        

                        the form
                        <!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:s="http://jboss.com/products/seam/taglib"
                         xmlns:h="http://java.sun.com/jsf/html">
                        <head>
                        <title>Test Upload</title>
                        </head>
                        <body>
                         <h:messages styleClass="message"/>
                         <h:form id="form" enctype="multipart/form-data">
                        
                         <h:panelGrid columns="2">
                         <h:outputText value="file name" />
                         <h:outputText value="#{uploadFile.fileName}" />
                        
                         <h:outputText value="file size" />
                         <h:outputText value="#{uploadFile.size}" />
                        
                        
                         <h:outputText value="file size" />
                         <h:inputTextarea value="#{uploadFile.byteArray}" />
                        
                         <s:fileUpload data="#{uploadFile.byteArray}" fileName="#{uploadFile.fileName}" fileSize="#{uploadFile.size}"/>
                         <h:commandButton action="#{uploadFile.reset}" value="reset"></h:commandButton>
                         </h:panelGrid>
                         </h:form>
                         </body>
                        </html>
                        

                        what to expect
                         * i'm here setByteArray([B@12e0415); count:1
                         * i'm here set File name = IMG_3332.jpg
                         * i'm here set File size = 1647973
                         * i'm here setByteArray(null); count:2
                        


                        I was get NPE when trying to access the 'byteArray' an couldn't figure out why. Since the fileName and size where set ok.

                        Is this a bug ?


                        • 9. Re: Help on s:filleupload

                           

                          <h:inputTextarea value="#{uploadFile.byteArray}" />
                          <s:fileUpload data="#{uploadFile.byteArray}" fileName="#{uploadFile.fileName}" fileSize="#{uploadFile.size}"/>
                          


                          Is this intended?

                          Regards

                          Felix

                          • 10. Re: Help on s:filleupload

                            I missed that. now it's ok. thanks