943,865 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3103
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 22nd, 2008
0

Getting a null pointer exception when saving image to file.

Expand Post »
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.

JAVA Syntax (Toggle Plain Text)
  1. package ColorForms;
  2.  
  3. import javax.swing.border.*;
  4. import java.awt.*;
  5. import java.awt.event.*;
  6. import java.awt.geom.*;
  7. import java.awt.image.BufferedImage;
  8. import java.io.*;
  9. import javax.swing.*;
  10. import java.util.*;
  11. import javax.imageio.ImageIO;
  12.  
  13.  
  14.  
  15. public class ColorForms extends JFrame
  16. {
  17. LeftPanel leftPanel;
  18. TopPanel topPanel;
  19. CanvasPanel canvasPanel;
  20. JSplitPane wholePanel, bottomPanel;
  21.  
  22. public static void main (String args[])
  23. {
  24. ColorForms cf = new ColorForms ();
  25. }
  26.  
  27.  
  28. public ColorForms ()
  29. {
  30. this.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  31. this.setSize (400, 400);
  32.  
  33. leftPanel = new LeftPanel ();
  34. canvasPanel = new CanvasPanel ();
  35. topPanel = new TopPanel (canvasPanel);
  36.  
  37. bottomPanel = new JSplitPane (JSplitPane.HORIZONTAL_SPLIT, leftPanel, canvasPanel);
  38. bottomPanel.setDividerLocation (50);
  39. wholePanel = new JSplitPane (JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
  40. wholePanel.setDividerLocation (70);
  41.  
  42. this.getContentPane ().add (wholePanel);
  43. this.setVisible (true);
  44. canvasPanel.repaint ();
  45. }
  46. }
  47.  
  48.  
  49. class LeftPanel extends JPanel
  50. {
  51. public LeftPanel ()
  52. {
  53. }
  54. }
  55.  
  56.  
  57. class TopPanel extends JPanel implements ActionListener
  58. {
  59. JButton createNewImageButton;
  60. JButton saveImageToFileButton;
  61. JButton retrieveImageFromFileButton;
  62. CanvasPanel cp;
  63.  
  64. public TopPanel (CanvasPanel canPan)
  65. {
  66. cp = canPan;
  67. createNewImageButton = new JButton ("Create New Image");
  68. saveImageToFileButton = new JButton ("Save Image To File");
  69. retrieveImageFromFileButton = new JButton ("Retrieve Image From File");
  70. this.add (createNewImageButton);
  71. this.add (saveImageToFileButton);
  72. this.add (retrieveImageFromFileButton);
  73. createNewImageButton.addActionListener(this);
  74. saveImageToFileButton.addActionListener(this);
  75. retrieveImageFromFileButton.addActionListener(this);
  76. }
  77.  
  78.  
  79. public void actionPerformed(ActionEvent e)
  80. {
  81. if (e.getSource () == createNewImageButton)
  82. CreateNewImage ();
  83. else if (e.getSource () == saveImageToFileButton)
  84. SaveImageToFile();
  85. else if (e.getSource () == retrieveImageFromFileButton)
  86. RetrieveImageFromFile ();
  87.  
  88. cp.repaint ();
  89. }
  90.  
  91.  
  92. public void CreateNewImage ()
  93. {
  94. cp.bi = new BufferedImage (200, 200, BufferedImage.TYPE_INT_ARGB_PRE);
  95. Color color1 = new Color(0, 0, 255, 255);
  96. Color color2 = new Color(255, 0, 0, 100);
  97. int rgb1 = color1.getRGB();
  98. int rgb2 = color2.getRGB();
  99. for (int i = 0; i < 50; i++)
  100. {
  101. for (int j = 0; j < 50; j++)
  102. {
  103. cp.bi.setRGB(i, j, rgb2);
  104. }
  105. }
  106. }
  107.  
  108.  
  109. public void SaveImageToFile ()
  110. {
  111. try
  112. {
  113. File saveFile = new File("SemiTransparentSquare.gif");
  114. if (saveFile == null)
  115. System.out.println ("saveFile is null");
  116. if (cp.bi == null)
  117. System.out.println ("cp.bi is null");
  118. ImageIO.write(cp.bi, "gif", saveFile);
  119. }
  120. catch (IllegalArgumentException ex)
  121. {
  122. System.out.print ("Caught an illegal argument exception - ");
  123. System.out.println (ex.toString());
  124. }
  125. catch (IOException ex)
  126. {
  127. System.out.print ("Caught an IO exception - ");
  128. System.out.println (ex.toString());
  129. }
  130. catch (NullPointerException ex)
  131. {
  132. System.out.print ("Caught a null pointer exception - ");
  133. System.out.println (ex.toString());
  134. }
  135. catch (Exception ex)
  136. {
  137. System.out.print ("Caught an exception - ");
  138. System.out.println (ex.toString());
  139. }
  140. }
  141.  
  142.  
  143. public void RetrieveImageFromFile ()
  144. {
  145. try
  146. {
  147. File biFile = new File("SemiTransparentSquare.gif");
  148. cp.bi = ImageIO.read(biFile);
  149. }
  150. catch (IOException ex)
  151. {
  152. System.out.println ("Problem opening file");
  153. }
  154. }
  155. }
  156.  
  157.  
  158. class CanvasPanel extends JPanel
  159. {
  160. BufferedImage bi;
  161.  
  162.  
  163. public CanvasPanel ()
  164. {
  165. }
  166.  
  167.  
  168. public void paintComponent (Graphics g)
  169. {
  170. super.paintComponent (g);
  171. Graphics2D g2 = (Graphics2D) g;
  172. g2.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  173. setBackground(Color.GREEN);
  174. g2.setColor(Color.WHITE);
  175. g2.fillOval (50, 50, 75, 75);
  176. g2.drawImage (bi, 50, 50, null);
  177. }
  178. }
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 22nd, 2008
0

Re: Getting a null pointer exception when saving image to file.

if (cp.bi == null) What if cp is null ?

Try to print everything before you run anything:
Java Syntax (Toggle Plain Text)
  1. File saveFile = new File("SemiTransparentSquare.gif");
  2.  
  3. System.out.println("saveFile: "+saveFile);
  4.  
  5. if (saveFile == null)
  6. System.out.println ("saveFile is null");
  7.  
  8. System.out.println("cp: "+cp);
  9. System.out.println("cp.bi: "+cp.bi);
  10.  
  11. if (cp.bi == null)
  12. System.out.println ("cp.bi is null");
  13. ImageIO.write(cp.bi, "gif", saveFile);
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 22nd, 2008
0

Re: Getting a null pointer exception when saving image to file.

Click to Expand / Collapse  Quote originally posted by javaAddict ...
if (cp.bi == null) What if cp is null ?

Try to print everything before you run anything:
Java Syntax (Toggle Plain Text)
  1. File saveFile = new File("SemiTransparentSquare.gif");
  2.  
  3. System.out.println("saveFile: "+saveFile);
  4.  
  5. if (saveFile == null)
  6. System.out.println ("saveFile is null");
  7.  
  8. System.out.println("cp: "+cp);
  9. System.out.println("cp.bi: "+cp.bi);
  10.  
  11. if (cp.bi == null)
  12. System.out.println ("cp.bi is null");
  13. ImageIO.write(cp.bi, "gif", saveFile);

That's debugging code. cp.bi shouldn't be null (I should actually have it return from the function if it is). I added those lines in to demonstrate that they don't display, yet I still get a null pointer exception even though neither cp.bi is null, nor is saveFile null. So what could be null?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 22nd, 2008
0

Re: Getting a null pointer exception when saving image to file.

Give me a minute to run the code
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 22nd, 2008
1

Re: Getting a null pointer exception when saving image to file.

Well for starters it always helps when debugging to add a ex.printStackTrace() in the catch:
Java Syntax (Toggle Plain Text)
  1. System.out.print ("Caught a null pointer exception - ");
  2. System.out.println (ex.toString());
  3. ex.printStackTrace();

After doing so, I noticed that none of your aruments is null. This is what I got:

Quote ...
java.lang.NullPointerException
at com.sun.imageio.plugins.common.PaletteBuilder.findPaletteEntry(PaletteBuilder.java:310)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexColorModel(PaletteBuilder.java:296)
at com.sun.imageio.plugins.common.PaletteBuilder.getIndexedImage(PaletteBuilder.java:145)
at com.sun.imageio.plugins.common.PaletteBuilder.createIndexedImage(PaletteBuilder.java:77)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:564)
at com.sun.imageio.plugins.gif.GIFImageWriter.write(GIFImageWriter.java:492)
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)

at ColorForms.TopPanel.SaveImageToFile(TopPanel.java:87)
at ColorForms.TopPanel.actionPerformed(TopPanel.java:46)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
I have never used these classes before so I am not sure how they need to be instantiated. I believe that maybe when the java methods are called
(from the stack trace:
at javax.imageio.ImageWriter.write(ImageWriter.java:598)
at javax.imageio.ImageIO.write(ImageIO.java:1479)
at javax.imageio.ImageIO.write(ImageIO.java:1521)
)
something that was not supposed to be is null when they use your arguments. I cannot say much so you would better wait for someone better to see this and figure it out

Sorry
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 22nd, 2008
1

Re: Getting a null pointer exception when saving image to file.

Null pointer exceptions are unchecked (the documentation will never say that a method can throw it). So it could be thrown from one of the methods you call.

Can you add ex.printStackTrace() to the section of code where you check the NullPointerException and then run the program again? That should help figure out what's going on.

EDIT: what javaAddict said
Last edited by brianlevine; Oct 22nd, 2008 at 4:52 am.
Reputation Points: 37
Solved Threads: 5
Light Poster
brianlevine is offline Offline
36 posts
since Oct 2008
Oct 22nd, 2008
1

Re: Getting a null pointer exception when saving image to file.

It may be possible that Java won't support an image-type in future releases and therefore not provide Serialization support for it but that depends on what it is.

I remember getting warning pertaining to images of a specific class and that class might be the culprit but I can't completely confirm this @_@. It's just a possibility >_<

-Alex
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Oct 22nd, 2008
2

Re: Getting a null pointer exception when saving image to file.

Try saving it to a .png instead of a .gif, that should (hopefully) work.
Last edited by jasimp; Oct 22nd, 2008 at 7:16 am.
Featured Poster
Reputation Points: 533
Solved Threads: 53
Senior Poster
jasimp is offline Offline
3,593 posts
since Aug 2007
Oct 22nd, 2008
0

Re: Getting a null pointer exception when saving image to file.

Click to Expand / Collapse  Quote originally posted by jasimp ...
Try saving it to a .png instead of a .gif, that should (hopefully) work.
I changed in the code this:
ImageIO.write(cp.bi, "gif", saveFile);
into this:
ImageIO.write(cp.bi, "png", saveFile); or
ImageIO.write(cp.bi, "jpeg", saveFile);
And it worked. Although the file saved in my file system has extension .gif ??
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,259 posts
since Dec 2007
Oct 22nd, 2008
1

Re: Getting a null pointer exception when saving image to file.

> And it worked. Although the file saved in my file system has extension .gif ??

The file name doesn't matter; its content does. I could have very well named it 'image.exe' and it still wouldn't have mattered. Maybe you overlooked the declaration File saveFile = new File("SemiTransparentSquare.gif"); in which the file name is pretty much a constant.
Last edited by ~s.o.s~; Oct 22nd, 2008 at 12:06 pm.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need help in JDBC Transaction
Next Thread in Java Forum Timeline: IE throws exception with Vista UAC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC