Rachel Rachel - 4 months ago 20
Java Question

java.lang.NullPointerException at FileUploadBean.uploadFile

I am getting

java.lang.NullPointerException at FileUploadBean.uploadFile
, not sure why as I have data in my list of string
fileData
.

Java code:

private List<String> fileData;

public void uploadFile(FileEntryEvent event) {
System.out.println("Event: " + event.getSource());
System.out.println("Test");
FileEntry fileEntry = (FileEntry) event.getSource();
FileEntryResults results = fileEntry.getResults();
for (FileEntryResults.FileInfo fileInfo : results.getFiles()) {
if (fileInfo.isSaved()) {
System.out.println("File is saved sucessfully");
fileData.add("FileName: " + fileInfo.getFileName());
fileData.add("FileSize: " + fileInfo.getSize());
fileData.add("FileStatus: " + fileInfo.getStatus());
}
}
}

public List<String> getFileData() {
System.out.println("inside getFileData");
List<String> data = fileData;
return fileData;
}

public void setFileData(List<String> fileData) {
this.fileData = fileData;
}


Exception:

Caused by: java.lang.NullPointerException
at FileUploadBean.uploadFile(FileUploadBean.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at org.icefaces.ace.component.fileentry.FileEntry.broadcast(FileEntry.java:306)
... 28 more

Answer

fileData is null, since you did not create it yet. Add the follwing to your uploadFile method:

fileData = new ArrayList<String>();

PS: The setFileData method you might not need.