在两个JFrame之间传递变量时出现EventQueue错误

时间:2014-01-20 02:58:27

标签: java swing variables netbeans jframe

我正在尝试在Java中的两个JFrame对象之间传递字符串,登录表单和菜单。

登录代码:

String username;
username = dbUser;
this.setVisible(false);
new Menu(username).setVisible(true);`

在菜单表单中,我有一个覆盖来用字符串

实例化该类
public class Menu extends javax.swing.JFrame {

private Component frame;
String user;

public Menu() {
    initComponents();
}

public Menu(String User) {
    user = User;
    lblCurrent.setText("Current User: " + user);
    initComponents();
}

现在,在Netbeans创建的public static void main中,以及外观代码,它有一个创建表单的事件处理程序。

public static void main(String args[]) {
    /* Set the Nimbus look and feel */
           /*OMMITED FROM THIS*/

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Menu().setVisible(true);
        }
    });

无论我做什么,我都无法为此添加覆盖功能,接受字符串并使其工作,因此当表单由登录表单创建时,我收到此错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Menu.<init>(Menu.java:26)
    at Login.btnLoginActionPerformed(Login.java:213)
    at Login.access$000(Login.java:21)
    at Login$1.actionPerformed(Login.java:54)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(BasicRootPaneUI.java:208)
    at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1664)
    at javax.swing.JComponent.processKeyBinding(JComponent.java:2878)
    at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:307)
    at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:250)
    at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2970)
    at javax.swing.JComponent.processKeyBindings(JComponent.java:2962)
    at javax.swing.JComponent.processKeyEvent(JComponent.java:2841)
    at java.awt.Component.processEvent(Component.java:6282)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1895)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:762)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1027)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:899)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:727)
    at java.awt.Component.dispatchEventImpl(Component.java:4731)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:708)
    at java.awt.EventQueue$4.run(EventQueue.java:706)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Java Result: 1

现在我可能完全搞砸了一些事情,或者有些事情直接在我脑海中浮现,但如果有人能够指出我正在做的那个愚蠢的错误,我会很感激。我之前从未使用过Netbeans,而且在T_T之前我从未见过这个错误。

1 个答案:

答案 0 :(得分:0)

由于您的应用程序应该以登录方式启动,因此不应该调用Menu类的main方法。

相反,应该使用Login类的(main方法)。您也可以从菜单类中删除main方法。

希望这有帮助。