0

this program calls another class when the credentials are passed into it. The issue is that an exception is occuring in the thread when the JFrame of the other class is being called (setVisible = 1).

Program:

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login window = new Login();
                    window.frmLogin.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }




    /**
     * Create the application.
     */
    public Login() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    public void initialize() {
        frmLogin = new JFrame();
        frmLogin.setTitle("Login");
        frmLogin.setSize(500,300);
        frmLogin.setResizable(false);
        frmLogin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmLogin.getContentPane().setLayout(null);

        user = new JTextField();
        user.setBounds(204, 90, 86, 20);
        frmLogin.getContentPane().add(user);
        user.setColumns(10);

        JLabel lblUsername = new JLabel("Username:");
        lblUsername.setBounds(113, 90, 81, 20);
        frmLogin.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password:");
        lblPassword.setBounds(113, 138, 81, 20);
        frmLogin.getContentPane().add(lblPassword);

        login = new JButton("Login");

        login.addMouseListener(new MouseAdapter() {
            @Override




            public void mouseClicked(MouseEvent arg0) {
                String b = new String(pass.getPassword());

                AdDAuthenticator adAuthenticator = new AdDAuthenticator();
                Map<String,Object> attrs = adAuthenticator.authenticate(user.getText(), b);

                if (attrs != null) {
                    for (String attrKey : attrs.keySet()) {
                        if (attrs.get(attrKey) instanceof String) {
                            System.out.println(attrKey +": "+attrs.get(attrKey));
                        } else {
                            System.out.println(attrKey +": (Multiple Values)");
                            for (Object o : (HashSet<?>)attrs.get(attrKey)) {
                                System.out.println("\t value: " +o);
                            }
                        }
                    }
                    updateScreen upd = new updateScreen();
                    frmLogin.dispose();   
                    upd.frm.setVisible(true);  //exception gets called.
                } else {
                    System.out.println("Attributes are null!");
                    System.out.println(b);
                    System.out.println(user.getText());
                    JOptionPane.showMessageDialog(null, "Credentials are Incorrect", "Error" , JOptionPane.WARNING_MESSAGE);
                    System.exit(0);
                }

            }
        });
        login.setBounds(165, 195, 89, 23);
        frmLogin.getContentPane().add(login);

        pass = new JPasswordField();
        pass.setBounds(204, 138, 86, 20);
        frmLogin.getContentPane().add(pass);

        lbljmawirelesscom = new JLabel("@.com");
        lbljmawirelesscom.setBounds(300, 92, 110, 17);
        frmLogin.getContentPane().add(#####);

    }







} 

the exception is given below:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Login$2.mouseClicked(Login.java:104)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

I am not sure as to why this is happening and i am not sure how to go about dealing with the exceptions. Let me know if you need more info.

Rahul
  • 903
  • 1
  • 10
  • 27
  • Based on your out-of-context code, I'm unable to reproduce your issue. I would, however recommend having a look at [How to Use Buttons](https://docs.oracle.com/javase/tutorial/uiswing/components/button.html), [How to Write an Action Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html) and [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) – MadProgrammer Jan 31 '19 at 20:46
  • @MadProgrammer Is my code very poorly written? – Rahul Jan 31 '19 at 20:47
  • 1
    It's not brilliant, it shows that you have little experience with Swing and haven't made much time to understand how the API works. For example, buttons should use `ActionListener`, not `MouseListener`, as many users will want to use the keyboard to trigger them, not the mouse – MadProgrammer Jan 31 '19 at 21:00
  • I am not a java guy, took up this project to learn it but for the actionlistener part - the computer has an onscreen keyboard and just a mouse for which the "software" is being made. Appreciate your input. – Rahul Jan 31 '19 at 21:09
  • For anyone who stumbles upon this. The issue was that the button had to be declared as a class member. The mouseclicked event could not see the button which caused the issue. Created the listener pointing to the (newly declared) button and declared the mouse event outside the initialize function. – Rahul Feb 01 '19 at 21:48

0 Answers0