我遇到了一个问题,当我运行我的应用程序并单击JMenuBar时,我得到以下异常,我不确定为什么因为异常指向我的代码没有行,这些行都来自JavaLibraries。 事件处理程序/ Dispatche上的异常点...所以我猜可能我没有在JMenuBar,JMenu和JMenuItem上设置配置。
基本上是我的代码:
menubarsita = new JMyMenuBar(); // Instanciate the JMenuBar
session.buildMenu(menubarsita,this); //Add the JMenu and the JMenuItems
this.setJMenuBar(menubarsita); // Add the menu bar to the JFrame
添加JMenu和菜单项的代码:
JMenu archivo = new JMenu(); //Instanciate the JMenu
archivo.setText("archivo"); // Set Text
MenuItem arch = new JMenuItem(); //Instanciate the JMenuItem
arch.setText("lala"); // Set Text
arch.addActionListener(frame); //Adding the action listener.
archivo.add(arch); // Adding JMenuItem To JMenu
menubar.add(archivo); // Adding JMenu to JMenuBar
异常详细信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.plaf.basic.BasicMenuBarUI$Handler.stateChanged(BasicMenuBarUI.java:176)
at javax.swing.DefaultButtonModel.fireStateChanged(DefaultButtonModel.java:333)
at javax.swing.DefaultButtonModel.setSelected(DefaultButtonModel.java:215)
at javax.swing.JMenu.setSelected(JMenu.java:294)
at javax.swing.JMenu.menuSelectionChanged(JMenu.java:1175)
at javax.swing.MenuSelectionManager.setSelectedPath(MenuSelectionManager.java:100)
at javax.swing.plaf.basic.BasicMenuUI$Handler.mousePressed(BasicMenuUI.java:434)
at java.awt.Component.processMouseEvent(Component.java:6285)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6053)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4235)
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:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
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:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
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)
更新
如果添加JMenu和Menu项的代码重新排列如下,则不会出现异常:
JMenu archivo = new JMenu();
JMenuItem arch = new JMenuItem();
archivo.setText("archivo");
arch.setText("lala");
arch.addActionListener(frame);
archivo.add(arch);
menubar.add(archivo);