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

JMenu's method remove(JMenuItem mi) throws NPE

XMLWordPrintable

    • 1.2.2
    • generic, x86, sparc
    • generic, solaris_2.5, solaris_7, solaris_8, windows_95, windows_nt



      Name: vsC58871 Date: 08/27/98


      com.sun.java.swing.JMenu's method remove(JMenuItem mi)
      throws NullPointerException if any Action is added to JMenu.

      NullPointerException is thrown from this code:

      if (listenerRegistry != null) {
          ActionChangedListener p = (ActionChangedListener)listenerRegistry.remove(item);
          Action a = (Action)listenerRegistry.remove(p);
        ...

      when item is a JMenuItem, so p = null and
      attempt to remove null from listenerRegistry causes NPE.

      Here is the example demonstrating the bug:
      ----------------Test.java---------------------
      import com.sun.java.swing.*;
      import java.awt.event.*;
      import java.beans.*;

      public class Test {
          public static void main(String[] args){
      JMenu menu = new JMenu();
      JMenuItem mi = new JMenuItem("VVS");
      Action a = new StubAction();
      menu.add(a);
      menu.add(mi);
      menu.remove(mi); // <--------------
      System.exit(0);
          }
      }
      class StubAction implements Action {
          String name = "StubAction";
          public void actionPerformed(ActionEvent e) {}
          public String getText(String key) { return "StubAction"; }
          public void setText(String key, String newValue) {}
          public Icon getIcon(String key) { return null; }
          public void setIcon(String key, Icon newValue) {}
          public boolean isEnabled() { return true; }
          public void setEnabled(boolean newValue) {}
          public void addPropertyChangeListener(PropertyChangeListener listener) {}
          public void removePropertyChangeListener(PropertyChangeListener listener) {}
          public void putValue(String str, Object o) {}
          public Object getValue(String str) {
      if(str.equals(Action.NAME))
      return name;
      else
      return null;
          }
      }
      -- The output ----------------
      #>java -version
      java version "1.2fcs"
      Classic VM (build JDK-1.2fcs-G, green threads, sunwjit)
      #>JAVA_COMPILER=none java Test
      Warning: JIT compiler "none" not found. Will use interpreter.
      Exception in thread "main" java.lang.NullPointerException
              at java.util.Hashtable.remove(Hashtable.java:421)
              at com.sun.java.swing.JMenu.remove(JMenu.java:581)
              at Test.main(Test.java:12)
      ------------------------------

      ======================================================================

            gsaab Georges Saab
            vsizikov Vladimir Sizikov
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: