Domanda

I'm re-programming one of my old login screen codes and I am changing the:

JPasswordField.getText(); 

To:

JPasswordFieald.getPassword();

Which is a lot harder because it outputs charecters.

Anyway, JFrame.dispose() isnt working for me. I want my program to dispose an old JFrame to a new JFrame (e.e JFrame2).

Here is my code:

public class Launcher {

    //Define Variables
    public static String VER = "1.1.0";
    public static String STATE = " ALPHA ";
    public static JFrame launcher;
    public static char[] sPass;
    public static String sUser;

    //Create widgets
    public static JTextField User = new JTextField();
    public static JPasswordField Pass = new JPasswordField();
    public static JButton Login = new JButton("Login");

    //Runs when program starts
    public static void main(String[] args) {
        NewFrame("Infinite Doom Launcher");

        //Checks if login has been pressed
        Login.addActionListener(new ActionListener(){

             public void actionPerformed(ActionEvent e){
                 sUser = User.getText();
                 sPass = Pass.getPassword();

                 //checks if password is correct
                 if(CheckPass(sPass)) {
                     if(sUser.equals("genfy")){
                        Game.main(null);
                        launcher.dispose();
                     }
                 }
             }
        });

    }

    //Creates new frame
    public static void NewFrame(String Name) {
        JFrame launcher = new JFrame(Name + " " + STATE + VER);
        launcher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        launcher.setSize(500,95);
        launcher.setResizable(false);

        //Add widgets to a Border Layout
        launcher.setLayout(new BorderLayout());
        launcher.add(User, BorderLayout.NORTH);
        launcher.add(Pass, BorderLayout.CENTER);
        launcher.add(Login, BorderLayout.SOUTH);

        //Set visible
        launcher.setVisible(true);
    }

    //Checks if the entered password is correct
    private static boolean CheckPass(char[] input) {
        boolean isCorrect = true;
        char[] correctPassword = { 'g', 'e', 'n', 'f', 'y', 'g', 'e', 'n', 'y', 's' };

        if (input.length != correctPassword.length) {
            isCorrect = false;
        } else {
            isCorrect = Arrays.equals (input, correctPassword);
        }

        //Zero out the password.
        Arrays.fill(correctPassword,'0');

        return isCorrect;
    }
}

So the problem seems to be occurring at:

             //checks if password is correct
             if(CheckPass(sPass)) {
                 if(sUser.equals("genfy")){
                    Game.main(null);
                    launcher.dispose();
                 }

specifically:

launcher.dispose();

So when I press JButton(login) this error appears:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at main.Launcher$1.actionPerformed(Launcher.java:42)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(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$200(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$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.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$1.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 apologize for my code being so long.

È stato utile?

Soluzione

It seems there is a scope problem in NewFrame method at this line:

JFrame launcher = new JFrame(Name + " " + STATE + VER);

This launcher variable is local to NewFrame method and is hidding launcher class member. So when you call launcher.dispose() within actionPerformed() method you get a NullPointerException. To solve this you should do this change:

launcher = new JFrame(Name + " " + STATE + VER);

And also take a look to this topic: The Use of Multiple JFrames, Good/Bad Practice?

Off-topic

As @kleopatra has suggested, learn about java naming conventions and stick to them. See more in this document: Java Code Conventions

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top