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

MenuShortcut causes NullPointerException on ActionEvent.getActionCommand()

XMLWordPrintable

    • generic
    • generic



      Name: el35337 Date: 05/18/98


      1. Create a menu item with a shortcut.
      2. In actionPerformed, attempt to use the getActionCommand() on the event.
      3. If the MenuItem is activated via mouse, no problem.
      4. If the MenuItem is activated via shortcut key, call to getActionCommand() throws NullPointerException.

      import java.awt.*;
      import java.awt.event.*;

      public class FooBar extends Frame implements ActionListener
      {
      public static void main(String[] args)
      {
      FooBar f = new FooBar();
      f.pack();
      f.show();
      }

      public FooBar()
      {
      MenuBar mb = new MenuBar();
      Menu m = new Menu("File");
      MenuShortcut ms = new MenuShortcut((int) "O".charAt(0));
      MenuItem mi = new MenuItem("Open", ms);
      mi.addActionListener(this);

      m.add(mi);
      mb.add(m);
      setMenuBar(mb);
      }

      public void actionPerformed(ActionEvent e)
      {
      // Here's the problem. If I click on Open,
      // I get the expected response. If I use the
      // shortcut, I get a NullPointerException due to
      // the call to getActionCommand.
      if (e.getActionCommand().equals("Open"))
      {
      System.out.println("Yeah, I'll think about it");
      }
      }
      }


      (Review ID: 30098)
      ======================================================================

            feckssunw Fred Ecks (Inactive)
            elarsen Erik Larsen (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: