richfaces 4.3 fileUpload java.lang.NullPointerException
morellik Jun 26, 2013 7:14 AMDear all,
I'm a newbie with richfaces. Now I'm trying to use fileUpload but I obtain NullPointerException error. I'm using RichFaces 4.3.2 with GlassFish 3.1.2.2 and NetBeans 7.3.
This is the form:
<h:form> <h:panelGrid columns="2" columnClasses="top,top"> <rich:fileUpload fileUploadListener="#{fileUploadBean.listener(event)}" id="upload" acceptedTypes="txt,csv, pdf" ontyperejected="alert('Only Text and CSV files are accepted');" maxFilesQuantity="5"> <a4j:ajax event="uploadcomplete" execute="@none" render="info" /> </rich:fileUpload> </h:panelGrid> <h:panelGroup id="info"> <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="image/jpeg" 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> <a4j:commandButton action="#{fileUploadBean.clearUploadData}" render="info, upload" value="Clear Uploaded Data" rendered="#{fileUploadBean.size>0}" /> <a4j:commandButton action="#{fileUploadBean.writeFile}" value="Write file to Disk" rendered="#{fileUploadBean.size>0}" /> </h:panelGroup> </h:form>
The following is the listener:
@ManagedBean @SessionScoped public class FileUploadBean implements Serializable { private static final long serialVersionUID = -4664762090820133359L; private ArrayList<UploadedText> files = new ArrayList<UploadedText>(); private static final String fPath = "/tmp"; public FileUploadBean() { } public void paint(OutputStream stream, Object object) throws IOException { stream.write(getFiles().get((Integer) object).getData()); stream.close(); } public void listener(FileUploadEvent event) throws Exception { UploadedFile item = event.getUploadedFile(); UploadedText file = new UploadedText(); System.out.println(item.getData().length); file.setLength(item.getData().length); file.setName(item.getName()); file.setData(item.getData()); files.add(file); } public String clearUploadData() { files.clear(); return null; } public int getSize() { if (getFiles().size() > 0) { return getFiles().size(); } else { return 0; } } public long getTimeStamp() { return System.currentTimeMillis(); } public ArrayList<UploadedText> getFiles() { return files; } public void setFiles(ArrayList<UploadedText> files) { this.files = files; } public void writeFile() { FileOutputStream fop = null; File file; UploadedText contain = files.size() > 0 ? files.get(0) : null; if (contain == null) { return; } try { file = new File(fPath + contain.getName()); fop = new FileOutputStream(file); if (!file.exists()) { file.createNewFile(); } fop.write(contain.getData()); fop.flush(); fop.close(); } catch (IOException e) { } finally { try { if (fop != null) { fop.close(); } } catch (IOException e) { } } }
And the following is the UploadedText class.
public class UploadedText implements Serializable { private static final long serialVersionUID = -3957467715082208719L; private String name; private String mime; private long length; private byte[] data; public String getName() { return name; } public void setName(String name) { int extDot = name.lastIndexOf('.'); if (extDot > 0) { String extension = name.substring(extDot + 1); if ("txt".equals(extension)) { mime = "text/plain"; } else if ("csv".equals(extension)) { mime = "text/csv"; } else if ("pdf".equals(extension)) { mime = "application/pdf"; } else { mime = "text/unknown"; } } this.name = name; } // Follow getter and setter
When I click on upload button appears the progress bar and immediately disappears and in the glassfish log I see:
SEVERE: JSF1073: javax.faces.event.AbortProcessingException caught during processing of APPLY_REQUEST_VALUES 2 : UIComponent-ClientId=j_idt24:upload, Message=/file/upload.xhtml @20,54 fileUploadListener="#{fileUploadBean.listener(event)}": java.lang.NullPointerException
SEVERE: /file/upload.xhtml @20,54 fileUploadListener="#{fileUploadBean.listener(event)}": java.lang.NullPointerException
Where I'm wrong?
Thanks