1 Reply Latest reply on Jun 17, 2009 2:44 AM by Aleksandr Dubovsky

    FileUpload problems

    leandro godoy Apprentice

      I am using the example of the rich liveDemo: Upload but it's always in nullPointerException method getData(), anyone know why? Look my code:

      
      package br.com.lgweb.crop.util;
      
      /**
       *
       */
      
      
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.ArrayList;
      
      import org.richfaces.event.UploadEvent;
      import org.richfaces.model.UploadItem;
      
      /**
       * @author Ilya Shaikovsky
       *
       */
      public class FileUploadBean{
      
       private ArrayList<File> files = new ArrayList<File>();
       private int uploadsAvailable = 5;
       private boolean autoUpload = false;
       private boolean useFlash = false;
       public int getSize() {
       if (getFiles().size()>0){
       return getFiles().size();
       }else
       {
       return 0;
       }
       }
      
       public FileUploadBean() {
       }
      
       public void paint(OutputStream stream, Object object) throws IOException {
       stream.write(getFiles().get((Integer)object).getData());
       }
       public void listener(UploadEvent event) throws Exception{
       UploadItem item = event.getUploadItem();
       File file = new File();
       file.setLength(item.getData().length);
       file.setName(item.getFileName());
       file.setData(item.getData());
       files.add(file);
       uploadsAvailable--;
       }
      
       public String clearUploadData() {
       files.clear();
       setUploadsAvailable(5);
       return null;
       }
      
       public long getTimeStamp(){
       return System.currentTimeMillis();
       }
      
       public ArrayList<File> getFiles() {
       return files;
       }
      
       public void setFiles(ArrayList<File> files) {
       this.files = files;
       }
      
       public int getUploadsAvailable() {
       return uploadsAvailable;
       }
      
       public void setUploadsAvailable(int uploadsAvailable) {
       this.uploadsAvailable = uploadsAvailable;
       }
      
       public boolean isAutoUpload() {
       return autoUpload;
       }
      
       public void setAutoUpload(boolean autoUpload) {
       this.autoUpload = autoUpload;
       }
      
       public boolean isUseFlash() {
       return useFlash;
       }
      
       public void setUseFlash(boolean useFlash) {
       this.useFlash = useFlash;
       }
      
      }
      
      
      




      file:
      
      package br.com.lgweb.crop.util;
      
      public class File {
      
       private String Name;
       private String mime;
       private long length;
       private byte[] data;
       public byte[] getData() {
       return data;
       }
       public void setData(byte[] data) {
       this.data = data;
       }
       public String getName() {
       return Name;
       }
       public void setName(String name) {
       Name = name;
       int extDot = name.lastIndexOf('.');
       if(extDot > 0){
       String extension = name.substring(extDot +1);
       if("bmp".equals(extension)){
       mime="image/bmp";
       } else if("jpg".equals(extension)){
       mime="image/jpeg";
       } else if("gif".equals(extension)){
       mime="image/gif";
       } else if("png".equals(extension)){
       mime="image/png";
       } else {
       mime = "image/unknown";
       }
       }
       }
       public long getLength() {
       return length;
       }
       public void setLength(long length) {
       this.length = length;
       }
      
       public String getMime(){
       return mime;
       }
      }
      
      
      




      my page

      
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://richfaces.org/rich" prefix="rich"%>
      <%@ taglib uri="http://richfaces.org/a4j" prefix="a4j"%>
      <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
      
      
      
      <html>
      <head>
      <title></title>
      </head>
      
      <body>
      <f:view>
      
      
       <style>
      .top {
       vertical-align: top;
      
      }
      .info {
       height: 202px;
       overflow: auto;
      }
      </style>
      
       <h:form>
       <h:panelGrid columns="2" columnClasses="top,top">
       <rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
       maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
       id="upload"
       immediateUpload="#{fileUploadBean.autoUpload}"
       acceptedTypes="jpg, gif, png, bmp" allowFlash="#{fileUploadBean.useFlash}">
       <a4j:support event="onuploadcomplete" reRender="info" />
       </rich:fileUpload>
       <h:panelGroup id="info">
       <rich:panel bodyClass="info">
       <f:facet name="header">
       <h:outputText value="Uploaded Files Info" />
       </f:facet>
       <h:outputText value="No files currently uploaded"
       rendered="#{fileUploadBean.size==0}" />
       <rich:dataGrid columns="1" value="#{fileUploadBean.files}"
       var="file" rowKeyVar="row">
       <rich:panel bodyClass="rich-laguna-panel-no-header">
       <h:panelGrid columns="2">
       <a4j:mediaOutput element="img" mimeType="#{file.mime}"
       createContent="#{fileUploadBean.paint}" value="#{row}"
       style="width:100px; height:100px;" cacheable="false">
       <f:param value="#{fileUploadBean.timeStamp}" name="time"/>
       </a4j:mediaOutput>
       <h:panelGrid columns="2">
       <h:outputText value="File Name:" />
       <h:outputText value="#{file.name}" />
       <h:outputText value="File Length(bytes):" />
       <h:outputText value="#{file.length}" />
       </h:panelGrid>
       </h:panelGrid>
       </rich:panel>
       </rich:dataGrid>
       </rich:panel>
       <rich:spacer height="3"/>
       <br />
       <a4j:commandButton action="#{fileUploadBean.clearUploadData}"
       reRender="info, upload" value="Clear Uploaded Data"
       rendered="#{fileUploadBean.size>0}" />
       </h:panelGroup>
       </h:panelGrid>
       </h:form>
      
      </f:view>
      </body>
      </html>
      
      
      



      please help me.
      Thank you.