I'm getting an error that I don't understand. It's a Null Pointer Exception, but neither of my arguments in line 118 is null. According to the documentation, ImageIO.write will throw a IllegalArgumentException or an IOException, but it isn't throwing either. I'm not sure what could be null here. You can run the program, then press the "Create New Image" button, then the "Save Image To File" button to get the error. Here is the code. The exception is caught on line 132. Lines 115 and 117 don't display. Lines 120 - 129 do not execute. Any ideas on what could cause the exception? Thanks.
package ColorForms;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.swing.*;
import java.util.*;
import javax.imageio.ImageIO;
public class ColorForms extends JFrame
{
LeftPanel leftPanel;
TopPanel topPanel;
CanvasPanel canvasPanel;
JSplitPane wholePanel, bottomPanel;
public static void main (String args[])
{
ColorForms cf = new ColorForms ();
}
public ColorForms ()
{
this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
this.setSize (400, 400);
leftPanel = new LeftPanel ();
canvasPanel = new CanvasPanel ();
topPanel = new TopPanel (canvasPanel);
bottomPanel = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvasPanel);
bottomPanel.setDividerLocation (50);
wholePanel = new JSplitPane (JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
wholePanel.setDividerLocation (70);
this.getContentPane ().add (wholePanel);
this.setVisible (true);
canvasPanel.repaint ();
}
}
class LeftPanel extends JPanel
{
public LeftPanel ()
{
}
}
class TopPanel extends JPanel implements ActionListener
{
JButton createNewImageButton;
JButton saveImageToFileButton;
JButton retrieveImageFromFileButton;
CanvasPanel cp;
public TopPanel (CanvasPanel canPan)
{
cp = canPan;
createNewImageButton = new JButton ("Create New Image");
saveImageToFileButton = new JButton ("Save Image To File");
retrieveImageFromFileButton = new JButton ("Retrieve Image From File");
this.add (createNewImageButton);
this.add (saveImageToFileButton);
this.add (retrieveImageFromFileButton);
createNewImageButton.addActionListener(this);
saveImageToFileButton.addActionListener(this);
retrieveImageFromFileButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource () == createNewImageButton)
CreateNewImage ();
else if (e.getSource () == saveImageToFileButton)
SaveImageToFile();
else if (e.getSource () == retrieveImageFromFileButton)
RetrieveImageFromFile ();
cp.repaint ();
}
public void CreateNewImage ()
{
cp.bi = new BufferedImage (200, 200, BufferedImage.TYPE_INT_ARGB_PRE);
Color color1 = new Color(0, 0, 255, 255);
Color color2 = new Color(255, 0, 0, 100);
int rgb1 = color1.getRGB();
int rgb2 = color2.getRGB();
for (int i = 0; i < 50; i++)
{
for (int j = 0; j < 50; j++)
{
cp.bi.setRGB(i, j, rgb2);
}
}
}
public void SaveImageToFile ()
{
try
{
File saveFile = new File("SemiTransparentSquare.gif");
if (saveFile == null)
System.out.println ("saveFile is null");
if (cp.bi == null)
System.out.println ("cp.bi is null");
ImageIO.write(cp.bi, "gif", saveFile);
}
catch (IllegalArgumentException ex)
{
System.out.print ("Caught an illegal argument exception - ");
System.out.println (ex.toString());
}
catch (IOException ex)
{
System.out.print ("Caught an IO exception - ");
System.out.println (ex.toString());
}
catch (NullPointerException ex)
{
System.out.print ("Caught a null pointer exception - ");
System.out.println (ex.toString());
}
catch (Exception ex)
{
System.out.print ("Caught an exception - ");
System.out.println (ex.toString());
}
}
public void RetrieveImageFromFile ()
{
try
{
File biFile = new File("SemiTransparentSquare.gif");
cp.bi = ImageIO.read(biFile);
}
catch (IOException ex)
{
System.out.println ("Problem opening file");
}
}
}
class CanvasPanel extends JPanel
{
BufferedImage bi;
public CanvasPanel ()
{
}
public void paintComponent (Graphics g)
{
super.paintComponent (g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
setBackground(Color.GREEN);
g2.setColor(Color.WHITE);
g2.fillOval (50, 50, 75, 75);
g2.drawImage (bi, 50, 50, null);
}
}