突然得到:java.lang.IllegalArgumentException:image == null

时间:2013-12-01 18:42:19

标签: java file-io bufferedimage javax.imageio

我已经能够使用与我现在使用相同的代码保存输出图像,但是,因为我一直在更新我的应用程序,所以保存我的图像的代码现在已经破了某种方式。

如果我把它放在'组合'之后保存文件创建输出它工作正常,我已经使用它来确保应用程序实际上正常工作(它是)。我确实希望应用程序的用户能够控制文件名,以便不会覆盖任何内容,正如我所说,它之前工作正常。

我用来保存图片的代码在这里:

private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        String fileName = JOptionPane.showInputDialog(null, "Save as...","");
        save(fileName, combinedImage);
    } 

public static void save(String fileName, BufferedImage combinedImage) {
            try {
            File outputfile = new File(fileName+".png");
            ImageIO.write(combinedImage, "png", outputfile);
            JOptionPane.showMessageDialog(null, "File successfully saved as: "+outputfile);
            System.out.println("File saved successfully: "+outputfile);
            }
            catch(IOException e) {
            JOptionPane.showMessageDialog(null,"Save Image Error", "Save Image Error", WARNING_MESSAGE);
            }
    }

combinedImage是我希望保存为PNG图像的输出。


完整的错误消息也在这里:

Exception in thread "AWT-EventQueue-0" 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)
    at grey2RGB.save(grey2RGB.java:808)
    at grey2RGB.saveButtonActionPerformed(grey2RGB.java:721)
    at grey2RGB.access$2100(grey2RGB.java:29)
    at grey2RGB$22.actionPerformed(grey2RGB.java:440)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

第808行是

ImageIO.write(combinedImage, "png", outputfile);

第721行是

save(fileName, combinedImage);

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

这类问题通常是由多个名为combinedImage的变量(并使用错误的变量)引起的。例如,检查:

  • 您在不同的地方没有多个名为combinedImage的变量
  • 如果combinedImage是一个成员变量,那么您在同一个对象实例中使用它,而不管您希望这样做,

如果这没有帮助,那么拿出调试器并跟踪代码。