我已经能够使用与现在使用的相同的代码来保存输出图像,但是,当我更新应用程序时,似乎保存图像的代码现在已经以某种方式中断了。

如果我把它放到“合并”后保存文件以创建输出的状态下,它就可以正常工作,并且我已使用此方法来确保应用程序实际上正常工作。我确实希望应用程序的用户能够控制文件名,以使任何内容都不会被覆盖,而且正如我所说,它以前可以正常工作。

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

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);


任何帮助,将不胜感激!

最佳答案

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


在不同的地方没有多个称为combinedImage的变量
如果combinedImage是成员变量,则无论您希望在哪里使用同一对象的实例,都可以使用它


如果那没有帮助,请退出调试器并跟踪代码。

10-01 23:03