title: Google app engine: NullpointerException while storing image in Blobstore through a java servlet
body: <p>I'm trying to upload images via a JSP file upload and a validation servlet to the Blobstore. the JSP part is as follows:</p>
  
  <pre><code>&lt;form action="/testuploadmimevalidation?provider-key=testprovider" method="post" enctype="multipart/form-data"&gt;
      &lt;input type="text" name="foo"&gt;
      &lt;input type="file" name="myfile" &gt;
      &lt;input type="submit" value="Submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>The java class <code>TestUploadMimeValidation</code> is as follows:</p>
  
  <pre><code>public class TestUploadMimeValidation extends HttpServlet {
  
      private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
      private static final Logger log = Logger.getLogger(TestUploadMimeValidation.class.getName());
      private static final boolean PRODUCTION_MODE = SystemProperty.environment.value() == SystemProperty.Environment.Value.Production;
      private static final String URL_PREFIX = PRODUCTION_MODE ? "" : "http://127.0.0.1:8080";
  
      public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
          InputStream in = req.getInputStream();
          int formDataLength = req.getContentLength();
          byte dataBytes[] = new byte[formDataLength];
          int len;
          ByteArrayOutputStream bos = new ByteArrayOutputStream();
  
          while ((len = in.read(dataBytes, 0, formDataLength)) != -1)
              bos.write(dataBytes, 0, len);
  
          dataBytes = bos.toByteArray();
          String urlStr = URL_PREFIX + BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/testupload");
          URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService();
          HTTPRequest request = new HTTPRequest(new URL(urlStr), HTTPMethod.POST, FetchOptions.Builder.withDeadline(20.0));
          request.setHeader(new HTTPHeader("Content-Type", "multipart/form-data"));
          request.setPayload(dataBytes);
  
          System.out.println("step1");
  
          try {
              HTTPResponse response = urlFetch.fetch(request);
              System.out.println("step2");
          } catch (IOException e) {
  
          } catch (NullPointerException e) {
  
          }
  
          System.out.println("step3");
      }
  }
  </code></pre>
  
  <p>This class uploads the image to the Blobstore just fine, but I get a <code>NullPointerException</code>. the stacktrace is as follows:</p>
  
  <pre><code>WARNING: /_ah/upload/ag5tOGJ5dXMtZGV2ZWxvcHIbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGAIM
  java.lang.NullPointerException
          at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:143)
          at com.google.appengine.api.blobstore.dev.UploadBlobServlet.handleUpload(UploadBlobServlet.java:180)
          at com.google.appengine.api.blobstore.dev.UploadBlobServlet.access$000(UploadBlobServlet.java:72)
          at com.google.appengine.api.blobstore.dev.UploadBlobServlet$1.run(UploadBlobServlet.java:101)
          at java.security.AccessController.doPrivileged(Native Method)
          at com.google.appengine.api.blobstore.dev.UploadBlobServlet.doPost(UploadBlobServlet.java:98)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
          at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
          at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
          at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
          at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
          at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
          at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
          at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
          at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
          at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
          at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
          at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
          at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
          at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
          at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
          at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
          at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
          at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
          at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
          at org.mortbay.jetty.Server.handle(Server.java:326)
          at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
          at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
          at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
          at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
          at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
          at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
          at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
  </code></pre>
  
  <p>Because of this NPE my code in the <code>TestUpload.java</code> class does not get executed. The <code>println</code>s come out fine. It prints "step3". The image gets stored into the Datastore. But I just can't get the code in the <code>TestUpload</code> to run. Do you have any idea what might be causing the exception? I've tried to mess with the multipart, but wasn't very succesful. </p>
  
  <p>Any help with this issue would be really appreciated.</p>