title: How to save a image file in java?
body: <p>I'm developing a QR generator app in java.This is the generating code,</p>
  
  <pre><code>try {
  
              ByteArrayOutputStream out = QRCode.from(txt_input.getText()).to(ImageType.PNG).stream();
              ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  
              BufferedImage image = ImageIO.read(in);
  
              lbl_output.setIcon(null);
              lbl_output.setIcon(new ImageIcon(image));
  
              String pic = "image";
  
          } catch (Exception e) {
              JOptionPane.showMessageDialog(null, " Please enter valid text !");
          }
  </code></pre>
  
  <p>After generate the QR image I need to save it.So I used this code,</p>
  
  <pre><code>JFileChooser save = new JFileChooser();
  int response = save.showSaveDialog(null);
  
              if (response == JFileChooser.APPROVE_OPTION) {
                 try {
                      File fileToSave = new File(pic + ".png");
                      ImageIO.write(image, "PNG", fileToSave);
  
                  } catch (Exception e) {
  
                  }
               }
  </code></pre>
  
  <p>But there is an exception like,  </p>
  
  <pre><code>java.lang.IllegalArgumentException: image == null!
      at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
      at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
      at javax.imageio.ImageIO.write(ImageIO.java:1520)
  </code></pre>