Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8359398

ImageIcon.setImage can't handle null parameter

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 26
    • client-libs
    • None
    • source
    • minimal
    • SE

      Summary

      If ImageIcon.setImage() receives a null image as parameter, it should remove the currently displayed icon.

      Problem

      ImageIcon.setImage receives an Image object as parameter. If it is null, then method throws NullPointerException from the loadImage method after it creates MediaTracker to track loading the image, which is unnecessary since image is null.

      Solution

      Bail out early if null Image is passed. A null image does not render any icon.

      Specification

      javax.swing.ImageIcon

       /**
            * Creates an ImageIcon from an image object.
      +     * Setting a {@code null} image will not render any image icon.
            * If the image has a "comment" property that is a string,
      public ImageIcon (Image image) {
      
         /**
            * Sets the image displayed by this icon.
      +     * Setting a {@code null} image will not render any image icon.
            * @param image the image
            */
           public void setImage(Image image) {

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: