1

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&hellip; <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&hellip; <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)
Anup Sahu
  • 93
  • 1
  • 6

2 Answers2

2

The HTML submit method should be "post", not "get". See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1

Roman C
  • 49,761
  • 33
  • 66
  • 176
robermann
  • 1,722
  • 10
  • 19
1

You are doing a lot of weird things.

  1. Use POST method since you need a body (and you are not performing an idempotent operation... GET = read, POST = write, basically, though often they can be used the other way around);

  2. Use the defaultStack, or define a Stack of your;

  3. Along with the File, you should define two Strings for contentyType and filename;

  4. You can upload multiple files easy with the multiple attribute from a single <input type="file" /> element;

  5. but even if you want to use multiple elements, you can still post the files to a List instead than to different file objects. What if tomorrow yuo will need to upload 10 files ? or 100 ? You will need to come here, change the code, and release it, every time.

  6. Don't return SUCCESS from within a catch block without even adding an error with addActionError("Something bad happened");.

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Sorry the wierd things were just to check why the file stream was giving a null pointer error. Changing to defaultStack and method to post solved the issue. Thanks – Anup Sahu Dec 03 '14 at 11:36