title: How can I convert an Icon to an Image
body: <p>I'm trying to convert an Icon (<code>javax.swing.Icon</code>) to an Image (<code>java.awt.Image</code>) using this code:</p>
  
  <pre><code>private Image iconToImage(Icon icon)
  {
      if(icon instanceof ImageIcon)
      {
          return ((ImageIcon) icon).getImage();
      }
      else
      {
          BufferedImage image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
          icon.paintIcon(null, image.getGraphics(), 0, 0);
          return image;
      }
  }
  </code></pre>
  
  <p>The thing is, the <code>paintIcon</code> function throws a <code>NullPointerException</code> on the <code>image.getGraphics()</code>.</p>
  
  <p>For the record, the <code>icon</code> value is the default <code>CheckBox</code> icon (obtained via <code>UIManager.getIcon("CheckBox.icon")</code>)</p>
  
  <p>Here are the details of the exception thrown :</p>
  
  <pre><code>Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
      at com.sun.java.swing.plaf.windows.WindowsIconFactory$CheckBoxIcon.paintIcon(WindowsIconFactory.java:306)
      at utils.WarningRenderer.iconToImage(WarningRenderer.java:50)
      at utils.WarningRenderer.&lt;init&gt;(WarningRenderer.java:38)
      at deliveryexpress.DeliveryExpressView.setWarnings(DeliveryExpressView.java:278)
      at deliveryexpress.DeliveryExpressView.updateLists(DeliveryExpressView.java:218)
      at deliveryexpress.DeliveryExpressView.access$1100(DeliveryExpressView.java:47)
      at deliveryexpress.DeliveryExpressView$5.addCheck(DeliveryExpressView.java:183)
      at org.japura.gui.model.DefaultListCheckModel.fireCheckListModelListeners(Unknown Source)
      at org.japura.gui.model.DefaultListCheckModel.fireAddCheckListModelListeners(Unknown Source)
      at org.japura.gui.model.DefaultListCheckModel.addCheck(Unknown Source)
      at org.japura.gui.CheckList$1.mouseClicked(Unknown Source)
      at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:253)
      at java.awt.Component.processMouseEvent(Component.java:6292)
      at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
      at java.awt.Component.processEvent(Component.java:6054)
      at java.awt.Container.processEvent(Container.java:2041)
      at java.awt.Component.dispatchEventImpl(Component.java:4652)
      at java.awt.Container.dispatchEventImpl(Container.java:2099)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
      at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4247)
      at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
      at java.awt.Container.dispatchEventImpl(Container.java:2085)
      at java.awt.Window.dispatchEventImpl(Window.java:2478)
      at java.awt.Component.dispatchEvent(Component.java:4482)
      at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:644)
      at java.awt.EventQueue.access$000(EventQueue.java:85)
      at java.awt.EventQueue$1.run(EventQueue.java:603)
      at java.awt.EventQueue$1.run(EventQueue.java:601)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
      at java.awt.EventQueue$2.run(EventQueue.java:617)
      at java.awt.EventQueue$2.run(EventQueue.java:615)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
      at java.awt.EventQueue.dispatchEvent(EventQueue.java:614)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
  </code></pre>
  
  <p>If you need more details, just tell me, I'll edit my post to add them.</p>
  
  <p>Thanks!</p>