title: Struts 2 File Upload - Null Pointer Exception
body: <p>I'm trying to upload a file using Struts2 in conjunction with Spring. But, somehow after reaching my action class, my file, <code>filename</code> and file content type are all coming out as <code>null</code>. I tried searching for the problem but for no results. Below is the code for my file upload.</p>
  
  <p><strong><code>index.jsp</code>:</strong></p>
  
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;s:head /&gt;
  &lt;/head&gt;
   &lt;s:actionerror /&gt;
  &lt;s:form action="uploadAction" method="POST" enctype="multipart/form-data"&gt;
  
  &lt;s:file name="fileUpload" label="Choose File" size="40" /&gt;
  
  &lt;s:submit value="Upload" name="submit" /&gt;
  
  &lt;/s:form&gt;
  </code></pre>
  
  <p><strong>Struts.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;
  
      &lt;struts&gt;
          &lt;include file="struts-default.xml"/&gt;
          &lt;package name="a" extends="struts-default"&gt;
  
     &lt;action name="resultAction" class="ManagePlanDataFileUploadActionBean"   method="executeFileUploadDemo"&gt;
  
              &lt;interceptor-ref name="fileUpload"&gt;
                  &lt;param name="maximumSize"&gt;10240&lt;/param&gt;
                  &lt;param name="allowedTypes"&gt;text/plain&lt;/param&gt;
              &lt;/interceptor-ref&gt;                
              &lt;interceptor-ref name="defaultStack" /&gt;
      &lt;result name="success"&gt;displayResultsJSP&lt;/result&gt;
  
  &lt;/action&gt;
          &lt;/package&gt;
      &lt;/struts&gt;
  </code></pre>
  
  <p><strong>applicationContext-web.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/jee
      http://www.springframework.org/schema/jee/spring-jee-2.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;
      &lt;bean id="ManagePlanDataFileUploadActionBean" scope="prototype"
  
      class="com.hix.action.planmanagement.ManagePlanDataFileUploadAction"&gt;
  
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p><strong>ManagePlanUploadAction</strong></p>
  
  <pre class="lang-java prettyprint-override"><code>package com.hix.action.planmgmt;
  
  import java.io.File;
  
  import javax.servlet.http.HttpServletRequest;
  
  import org.apache.commons.io.FileUtils;
  
  import com.opensymphony.xwork2.ActionSupport;
  
  public class ManagePlanUploadAction extends ActionSupport{
  
        private HttpServletRequest request;
  
        private File fileUpload;
        private String fileUploadContentType;
        private String fileUploadFileName;
  
         public void setServletRequest(HttpServletRequest paramHttpServletRequest) {
               this.request = paramHttpServletRequest;
         }
  
         public HttpServletRequest getServletRequest() {
               return request;
         }
  
          public File getFileUpload() {
              return fileUpload;
          }
  
          public void setFileUpload(File fileUpload) {
              this.fileUpload = fileUpload;
          }
  
          public String getFileUploadContentType() {
              return fileUploadContentType;
          }
  
          public void setFileUploadContentType(String fileUploadContentType) {
              this.fileUploadContentType = fileUploadContentType;
          }
  
          public String getFileUploadFileName() {
              return fileUploadFileName;
          }
  
          public void setFileUploadFileName(String fileUploadFileName) {
              this.fileUploadFileName = fileUploadFileName;
          }
  
          public String executeFileUploadDemo() throws Exception {
              try {
                  String filePath = "C:/Myuploads2";
                  System.out.println("Server path:" + filePath);
                  File fileToCreate = new File(filePath, fileUploadFileName);
                  FileUtils.copyFile(fileUpload, fileToCreate);
                  } catch(Exception e) {
                      e.printStackTrace();
                      addActionError(e.getMessage());
                      return SUCCESS;
                  }       
                      System.out.println("File :" + fileUpload);
                      System.out.println("Filename : " + fileUploadFileName);
                      System.out.println("File type : " + fileUploadContentType);
                  return SUCCESS; 
          }
  }
  </code></pre>
  
  <p><strong>tiles-def.xml</strong></p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE tiles-definitions PUBLIC
         "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
         "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"&gt;
  
  &lt;tiles-definitions&gt;
  &lt;definition name="displayResultsJSP" template="/jsp/planmgmt/Result.jsp"&gt;
  
  &lt;put-attribute name="header" value="/jsp/template/defaultHeader.jsp" /&gt; 
  
  &lt;put-attribute name="footer" value="/jsp/template/defaultFooter.jsp" /&gt;
  
  &lt;/definition&gt;
  
  &lt;/tiles-definitions&gt;
  </code></pre>
  
  <p><strong>Result.jsp</strong></p>
  
  <pre class="lang-jsp prettyprint-override"><code>&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt;
  &lt;html&gt;
  
  &lt;body&gt;
     File Name : &lt;s:property value="fileUploadFileName"/&gt; 
    &lt;br/&gt;
     Content Type : &lt;s:property value="fileUploadContentType"/&gt; 
    &lt;br/&gt;
     File : &lt;s:property value="fileUpload"/&gt; 
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>