0

In my class i have these errors and I don't know how to get rid of them, so if you would help I would be very grateful:

 public class MainGUI extends JFrame {
        String username = "";
        String password = "";
        public MainGUI() {
            initComponents();
        }

    private void initComponents() {
        JLabel jLblLogin = new JLabel();
        JTextField jTFUsername = new JTextField();
        JLabel jLblUsername = new JLabel();
        JLabel jLblPassword = new JLabel();
        JButton jBtnLogin = new JButton();
        JPasswordField jPFPassword = new JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLblLogin.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLblLogin.setText("Login: ");

        jLblUsername.setText("Username: ");
        jLblPassword.setText("Password: ");

        jBtnLogin.setText("Ok");
        jBtnLogin.addActionListener(new java.awt.event.ActionListener() {
            @Override
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLblLogin)
                .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jLblUsername)
                .addComponent(jLblPassword))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                .addComponent(jTFUsername)
                .addComponent(jBtnLogin, javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jPFPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))))
                .addContainerGap(70, Short.MAX_VALUE)));
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jLblLogin)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jTFUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jLblUsername))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLblPassword)
                .addComponent(jPFPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(jBtnLogin)
                .addContainerGap(64, Short.MAX_VALUE)));

        pack();
    }

    private void jBtnLoginActionPerformed(java.awt.event.ActionEvent evt) {
        try {
            Login tryLogin = new Login();
            username = jTFUsername.getText();
            password = jPFPassword.getText();
            System.out.println(username + " " + password);
            switch (tryLogin.verifyUser(username, password)) {
                case "admin":
                    AdminFrame af = new AdminFrame();
                    af.setVisible(true);
                    this.setVisible(false);
                    break;
                case "user":
                    UserFrame uf = new UserFrame();
                    uf.setVisible(true);
                    uf.passParameter(jTFUsername.getText());
                    this.setVisible(false);
                    break;
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }





        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainGUI().setVisible(true);
            }
        });
    }
    private javax.swing.JButton jBtnLogin;
    private javax.swing.JLabel jLblLogin;
    private javax.swing.JLabel jLblPassword;
    private javax.swing.JLabel jLblUsername;
    private javax.swing.JPasswordField jPFPassword;
    private javax.swing.JTextField jTFUsername;
}

And the errors are the following:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at PresentationLayer.MainGUI.jBtnLoginActionPerformed(MainGUI.java:88)
    at PresentationLayer.MainGUI.access$000(MainGUI.java:15)
    at PresentationLayer.MainGUI$1.actionPerformed(MainGUI.java:42)
    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.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    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.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:688)
    at java.awt.EventQueue$3.run(EventQueue.java:686)
    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:702)
    at java.awt.EventQueue$4.run(EventQueue.java:700)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
    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)

Line 88 :

username = jTFUsername.getText();

Line 16 :

public class MainGUI extends JFrame {

Line 42 :

jBtnLoginActionPerformed(evt);

I know its a simple nullpointerexception but being a beginner and after trying some solutions I didn't manage to pull this through.

user3848412
  • 115
  • 3
  • 11

2 Answers2

3

Problem here is you have declared jTFUsername in your class

private javax.swing.JTextField jTFUsername;

but while instantiating, you created another in initComponents method which is creating widgets locally

JTextField jTFUsername = new JTextField();

You need to instantiate class level jTFUserName in order for your use. so you need to replace above line in initComponents method with

jTFUsername = new JTextField();

Same problem is with your other widget instantiation.

Sanjeev
  • 9,876
  • 2
  • 22
  • 33
1

try following by changing

 private void initComponents() {
        JLabel jLblLogin = new JLabel();
        JTextField jTFUsername = new JTextField();
        JLabel jLblUsername = new JLabel();
        JLabel jLblPassword = new JLabel();
        JButton jBtnLogin = new JButton();
        JPasswordField jPFPassword = new JPasswordField();

into

 private void initComponents() {
        jLblLogin = new JLabel();
        jTFUsername = new JTextField();
        jLblUsername = new JLabel();
        jLblPassword = new JLabel();
        jBtnLogin = new JButton();
        jPFPassword = new JPasswordField();