I am trying to upload 2 files using struts2...... But when i open the file in FileInputStream i get a NullPointerException as the file values are getting assigned as Null.
Upload.html
<form action="report.action" method="get" enctype="multipart/form-data">
<div class="form-group input-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">Browse… <input type="file" name="file1"></span></span>
<input type="text" class="form-control" readonly placeholder="Amdocs Report">
</div>
</div>
<div class="form-group input-group">
<div class="input-group">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">Browse… <input type="file" name="file2"></span></span>
<input type="text" class="form-control" readonly placeholder="ILC Report">
</div>
Action Class
private File file1;
private File file2;
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public File getFile2() {
return file2;
}
public void setFile2(File file2) {
this.file2 = file2;
}
public String execute() {
try {
FileInputStream file= new FileInputStream(file1);
System.out.println(file1);
System.out.println(file2);
generate(file1,file2);
return "success";
}catch(Exception ex) {
ex.printStackTrace();
return "success";
}
}
Struts.xml
<struts>
<package name="default" extends="struts-default">
<action name="report" class="com.o2.report.GenerateReport">
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
<result name="success">index.html</result>
</action>
</package>
I get the file1 and file2 values as null when i see in debug mode.
Error
[3/12/14 16:30:45:994 IST] 0000001d SystemErr R java.lang.NullPointerException
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at java.io.FileInputStream.<init>(FileInputStream.java:109)
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at com.o2.report.GenerateReport.execute(GenerateReport.java:39)
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:45)
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
[3/12/14 16:30:45:995 IST] 0000001d SystemErr R at java.lang.reflect.Method.invoke(Method.java:599)
[3/12/14 16:30:45:996 IST] 0000001d SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
[3/12/14 16:30:45:996 IST] 0000001d SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
[3/12/14 16:30:45:996 IST] 0000001d SystemErr R at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
[3/12/14 16:30:45:996 IST] 0000001d SystemErr R at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:167)