title: Base64 To ImageIO
body: <p>I'm trying to convert a base64 string to an <code>imageIO</code> and saving it. </p>
  
  <p>In a bigger context, I'm creating a script so users can upload an image, crop it and save it as their profile image. I get a <code>Base64</code> String from this cropped image, and sent it to the servlet. (Yes, I'm using Java Server Pages)</p>
  
  <p>Here is the problem:
  When i execute the following code, i get a null pointer on my variable Image in the method <code>decodeToImage</code>.</p>
  
  <p>I tried to use several other libraries to decode the base64 but with no results yet.
  I tried to use other image convert scripts but none worked. </p>
  
  <p>Also this code seems to me that is should work. Am i missing something?</p>
  
  <p>Thanks in advance!</p>
  
  <p>All reactions are welcome!</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
      processRequest(request, response);
  
      if(request.getParameter("upload-CroppedImage") != null){
  
  
          BufferedImage newImage = decodeToImage(request.getParameter("croppedImage"));
  
          File outputfile = new File("F:\\Users\\Joery v2\\Pictures\\saved.jpg");
          ImageIO.write(newImage, "jpg", outputfile);
      }
  }
  
  public static BufferedImage decodeToImage(String imageString) {
  
      try {
          byte[] imgBytes = parseBase64Binary(imageString);
           BufferedImage image = ImageIO.read(new ByteArrayInputStream(imgBytes));
           return image;
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>*This is code from the servlet.</p>
  
  <p>Stack Trace:</p>
  
  <pre><code>Info:   GameZ_Website was successfully deployed in 2.076 milliseconds.
  Warning:   StandardWrapperValve[CroppingImage]: Servlet.service() for servlet CroppingImage threw exception
  java.lang.IllegalArgumentException: image == null!
      at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
      at javax.imageio.ImageIO.getWriter(ImageIO.java:1592)
  </code></pre>