I am learning to use JSF 2.2 and the upload file tag.
So I created a basis xhtml file to upload a file in a bean:
<h:form id="form" enctype="multipart/form-data">
<h:inputFile id="file" value="#{fileupload.uploadedFile}" />
<h:commandButton value="Upload" action="#{fileupload.saveFile}" />
</h:form>
Here is the associated backing-bean :
@ManagedBean
@ViewScoped
public class Fileupload {
private Part uploadedFile; // getters + setters
private String folder = "c:\\files";
public void saveFile(){
try (InputStream input = uploadedFile.getInputStream()) { // <-- Nullpointer here
String fileName = uploadedFile.getSubmittedFileName();
Files.copy(input, new File(folder, fileName).toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
As soon as I submit the form i have this error :
Caused by: java.lang.NullPointerException
at com.pro.beans.Fileupload.saveFile(Fileupload.java:30)
So my guess is that the uploadFile
mutable is not set by the xhtml and so when I click on the button I got a NullpointerException
, but I do understand why is it not set cause for me it is conform to all the tutorial I saw.