1
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JTextField;

    public class login extends JFrame implements ActionListener {
        JTextField text1;
        JTextField text2;
        JButton button3;
        JButton button4;
        JButton button5;
        JFrame frame5;
        String username;
        String password;
        String a;
        login(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
    
        text1 = new JTextField();
        text1.setPreferredSize(new Dimension(250,50));
        text1.setFont(new Font("MV Boli",Font.PLAIN,15));
        
        button3 = new JButton("UserName");
        button3.setFont(new Font("MV Boli",Font.PLAIN,15));
            
        text2 = new JTextField();
        text2.setPreferredSize(new Dimension(250,50));
        text2.setFont(new Font("MV Boli",Font.PLAIN,15));
        
        button4 = new JButton("PassWord");
        button4.setFont(new Font("MV Boli",Font.PLAIN,15));
        button5 = new JButton("Submit");
        button5.setFont(new Font("MV Boli",Font.PLAIN,15));
        button5.addActionListener(this);
    
        this.add(button3);
        this.add(text1);
        this.add(button4);
        this.add(text2);
        this.add(button5);
        this.setSize(400,500);
        this.setVisible(true);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stu
        if(e.getSource()==button5) {
            
            frame5.dispose();
            username = text1.getText();
            password = text2.getText();         
        }       
    }       
}

I'm getting an error like

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at login.actionPerformed(login.java:61)
    at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
    at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
    at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
    at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
    at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:279)
    at java.desktop/java.awt.Component.processMouseEvent(Component.java:6636)
    at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3342)
    at java.desktop/java.awt.Component.processEvent(Component.java:6401)
    at java.desktop/java.awt.Container.processEvent(Container.java:2263)
    at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
    at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4548)
    at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
    at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
    at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
    at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
    at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I just want to close the current frame when i open the the next frame by calling a new method in the action listener, but I am not able to dispose it.

I also want to inherit the values of username & password from the get text method and use them in another class to verify the username, password by establishing JDBC connection with MySQL database. If I inherit the class when I simply try to print the values of username, password in the new class I am getting the output as null. null is actually getting printed on the output screen. Please tell me how do I inherit these values in another class by also disposing the current frame.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
gm lohith
  • 33
  • 4
  • 1
    You get `NullPointerException` because frame5 is never initialized and it is null. Infact you do not need it. Try `this.dispose()` or simply `dispose()`. – c0der Oct 25 '20 at 05:02
  • 2
    When posting a question or an answer consider adding a minimal example of your code (see [mre]) like [this one](https://repl.it/repls/MajorPushyExperiments#Main.java) – c0der Oct 25 '20 at 05:14
  • 1
    1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) In this specific case, ask for login details in a modal `JDialog` or a `JOptionPane` and get the details on the line immediately following setting the option pane or dialog visible. 3) You seem to misunderstand the meaning of 'inheritance'. That tag has been removed. .. – Andrew Thompson Oct 25 '20 at 06:00
  • 1
    .. 4) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. 5) For a password, use a `JPassWordField`. 6) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Oct 25 '20 at 06:00
  • 2
    .. 7) Don't extend (*actual* inheritance) windows (like `JFrame`) or containers or components unless necessary. There is no reason to do so, here. – Andrew Thompson Oct 25 '20 at 06:06

1 Answers1

2
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at login.actionPerformed(login.java:61) at 

The line causing this exception is:

    frame5.dispose();

frame5 field is never assigned so is null.

The quickest fix would be to change the line frame5.dispose(); to dispose();. It is recommended to avoid unnecessarily extending classes. So a better approach would be to remove the extends JFrame.

    JFrame frame5 = new JFrame();

(this. should be replaced by frame5. towards the end of the constructor.)

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
  • I wrote extensive (well, 7) comments including *not* using a frame for this, but when using multiple frames, `..setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` will cause a further problem (I.E. exit the JRE when the frame disappears). – Andrew Thompson Oct 25 '20 at 06:12