Getting a null pointer exception when saving image to file.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Getting a null pointer exception when saving image to file.

 
0
  #1
Oct 22nd, 2008
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,706
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

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

 
0
  #2
Oct 22nd, 2008
if (cp.bi == null) What if cp is null ?

Try to print everything before you run anything:
  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);
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

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

 
0
  #3
Oct 22nd, 2008
Originally Posted by javaAddict View Post
if (cp.bi == null) What if cp is null ?

Try to print everything before you run anything:
  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?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,706
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

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

 
0
  #4
Oct 22nd, 2008
Give me a minute to run the code
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,706
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

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

 
1
  #5
Oct 22nd, 2008
Well for starters it always helps when debugging to add a ex.printStackTrace() in the catch:
  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:

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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 34
Reputation: brianlevine is an unknown quantity at this point 
Solved Threads: 5
brianlevine brianlevine is offline Offline
Light Poster

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

 
1
  #6
Oct 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

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

 
1
  #7
Oct 22nd, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 3,584
Reputation: jasimp has a spectacular aura about jasimp has a spectacular aura about jasimp has a spectacular aura about 
Solved Threads: 52
Featured Poster
jasimp's Avatar
jasimp jasimp is offline Offline
Senior Poster

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

 
2
  #8
Oct 22nd, 2008
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.
"Argyou not with the hand you are dealt in cards or life." ---- Wizard and Glass
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,706
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 228
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

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

 
0
  #9
Oct 22nd, 2008
Originally Posted by jasimp View Post
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 ??
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,642
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 472
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
1
  #10
Oct 22nd, 2008
> 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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC