0

I'm attempting to pass a string between two JFrame objects in Java, a login form and a menu.

Login Code:

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

and in the menu form, I have an override to instantiate the class with the string

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();
}

Now, in the public static void main which Netbeans creates, along with the look and feel code, it had an event handler which creates the form.

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);
        }
    });

No matter what I do, I can't add an override function to this, to accept a string and make this work, and as a result when the form gets created by the login form, I get this error:

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

Now I'm probably messing something up totally, or something is going straight over my head, but I'd appreciate if someone could point out the stupid mistake I'm making. I've never used Netbeans before and I've never seen this error before T_T.

Community
  • 1
  • 1
Zack Cain
  • 35
  • 7
  • See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) The 2nd frame should probably be a modal `JDialog` or a `JOptionPane`. I haven't looked into it closely but I'd guess the `NullPointerException` is because the `login` frame is **not** modal. – Andrew Thompson Jan 20 '14 at 03:03

1 Answers1

0

Since your application is supposed to be started with login, you shouldn't call main method of Menu class.

Instead, that (main method) of Login class should be used. You might as well should remove the main method from menu class.

Hope this helps.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45