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

JMenuBar.bindingForKeyStrokeRecursive NPE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.2.0
    • 1.1.5, 1.2.0
    • client-libs
    • swing1.0.2
    • generic, x86
    • generic, windows_95, windows_nt

      Entering text into a JTextField when using a JMenu without a JMenuItem causes an exception to be thrown.

      This problem does not appear when at least one JMenuItem is present. It also does not appear if the user first touches the empty menu. Obviously the second solution is not a practical work around for user, but neither is the first since the menu in question is being used to manage open windows. At some point there may be any open windows. The error message generated is:

      java.lang.NullPointerException
              at com.sun.java.swing.JMenuBar.bindingForKeyStrokeRecursive(JMenuBar.java:559)
              at com.sun.java.swing.JMenuBar.bindingForKeyStroke(JMenuBar.java:532)
              at com.sun.java.swing.JComponent.processKeyBinding(JComponent.java:1345)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1402)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindings(JComponent.java:1391)
              at com.sun.java.swing.JComponent.processKeyEvent(JComponent.java:1277)
              at java.awt.Component.processEvent(Component.java:2173)
              at java.awt.Container.processEvent(Container.java:878)
              at java.awt.Component.dispatchEventImpl(Component.java:1773)
              at java.awt.Container.dispatchEventImpl(Container.java:923)
              at java.awt.Component.dispatchEvent(Component.java:1705)
              at java.awt.LightweightDispatcher.processKeyEvent(Container.java:1411)
              at java.awt.LightweightDispatcher.dispatchEvent(Container.java:1395)
              at java.awt.Container.dispatchEventImpl(Container.java:910)
              at java.awt.Window.dispatchEventImpl(Window.java:485)
              at java.awt.Component.dispatchEvent(Component.java:1705)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)
      Exception occurred during event dispatching:
      java.lang.NullPointerException
              at com.sun.java.swing.JMenuBar.bindingForKeyStrokeRecursive(JMenuBar.java:559)
              at com.sun.java.swing.JMenuBar.bindingForKeyStroke(JMenuBar.java:532)
              at com.sun.java.swing.JComponent.processKeyBinding(JComponent.java:1345)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1402)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:1409)
              at com.sun.java.swing.JComponent.processKeyBindings(JComponent.java:1391)
              at com.sun.java.swing.JComponent.processKeyEvent(JComponent.java:1281)
              at java.awt.Component.processEvent(Component.java:2173)
              at java.awt.Container.processEvent(Container.java:878)
              at java.awt.Component.dispatchEventImpl(Component.java:1773)
              at java.awt.Container.dispatchEventImpl(Container.java:923)
              at java.awt.Component.dispatchEvent(Component.java:1705)
              at java.awt.LightweightDispatcher.processKeyEvent(Container.java:1411)
              at java.awt.LightweightDispatcher.dispatchEvent(Container.java:1395)
              at java.awt.Container.dispatchEventImpl(Container.java:910)
              at java.awt.Window.dispatchEventImpl(Window.java:485)
              at java.awt.Component.dispatchEvent(Component.java:1705)
              at java.awt.EventDispatchThread.run(EventDispatchThread.java:63)


      The following code demonstrates the problem:

      import java.io.*;
      import java.util.*;

      import java.awt.*;
      import java.awt.event.*;
      import com.sun.java.swing.*;


      class MyTextField extends JFrame {
      protected JMenuBar menuBar = new JMenuBar();
      private JMenuItem quitItem;
      private JMenuItem anItem;

      /*
      *
      * Create MyTextField Menus
      *
      */

      private JMenu fileMenu = new JMenu("File", true);
      private JMenu windowMenu = new JMenu("Window", true);

      MyTextField() {
      super("Sandhya's Test Menu ");
      this.setSize(600, 100);
      this.addWindowListener(new WinAdapt());
      this.createMenus();
      }

      public void createMenus() {

      MenuItemListener menuItemListener = new MenuItemListener();

      createFileMenu();
      createWindowMenu();
      createMenuBar();

      setJMenuBar(menuBar);

      getContentPane().setLayout(new FlowLayout());

      JTextField textField = new JTextField(30);
      getContentPane().add(textField);

      quitItem.addActionListener(menuItemListener);

      //anItem.addActionListener(menuItemListener);
      }

      private void createFileMenu() {
      fileMenu.add(quitItem = new JMenuItem("Quit"));
      }

      private void createWindowMenu() {
      //windowMenu.add(anItem = new JMenuItem("Test item"));
      }

      private void createMenuBar() {
      menuBar.add(fileMenu);
      menuBar.add(windowMenu);
      }

      public static void main(String args[]) {
      MyTextField javaMiniEditor = new MyTextField();
      javaMiniEditor.setVisible(true);
      }

      private class WinAdapt extends WindowAdapter {
      public void windowClosing(WindowEvent event) {
      dispose();
      System.exit(0);
      }
      }

      private class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent event) {
      JMenuItem item = (JMenuItem) event.getSource();
      System.out.println(item.getText());

      if (item == quitItem) {
      dispose();
      System.exit(0);
      }
      }
      }
      }

      A test menu item has been added to this demo but it is currently comented out. As an additonal piece of investegation this ported to AWT and the problem does not seem to exist there.

      import java.io.*;
      import java.net.*;
      import java.util.*;



      class MyTextField extends Frame {
      protected MenuBar menuBar = new MenuBar();
      private MenuItem quitItem;
      private MenuItem anItem;

      /*
      *
      * Create MyTextField Menus
      *
      */

      private Menu fileMenu = new Menu("File", true);
      private Menu windowMenu = new Menu("Window", true);

      MyTextField() {
      super("Sandhya's Test Menu ");
      this.setSize(600, 100);
      this.addWindowListener(new WinAdapt());
      this.createMenus();
      }

      public void createMenus() {

      MenuItemListener menuItemListener = new MenuItemListener();

      createFileMenu();
      createWindowMenu();
      createMenuBar();

      setMenuBar(menuBar);

      setLayout(new FlowLayout());

      TextField textField = new TextField(30);
      add(textField);

      quitItem.addActionListener(menuItemListener);

      // anItem.addActionListener(menuItemListener);

      }

      private void createFileMenu() {
      fileMenu.add(quitItem = new MenuItem("Quit"));
      }

      private void createWindowMenu() {
      // windowMenu.add(anItem = new MenuItem("Test item"));
      }

      private void createMenuBar() {
      menuBar.add(fileMenu);
      menuBar.add(windowMenu);
      }

      public static void main(String args[]) {
      MyTextField javaMiniEditor = new MyTextField();
      javaMiniEditor.setVisible(true);
      }

      private class WinAdapt extends WindowAdapter {
      public void windowClosing(WindowEvent event) {
      dispose();
      System.exit(0);
      }
      }

      private class MenuItemListener implements ActionListener {
      public void actionPerformed(ActionEvent event) {
      MenuItem item = (MenuItem) event.getSource();
      System.out.println(item.getLabel());

      if (item == quitItem) {
      dispose();
      System.exit(0);
      }
      }
      }
      }

            gsaab Georges Saab
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: