帧处于活动状态,但设置为false = nullpointer可见

时间:2014-09-24 07:21:41

标签: java nullpointerexception

所以我有一个奇怪的问题,我刚发布并修复了我的其他错误,但这似乎创造了这个,这没有任何意义。

基本上,用户按下一个按钮,此窗口打开,然后无论是按取消还是注册,此窗口都应该设置为false。但是当执行此代码时,它表示我的窗口为空。

package frontend;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Registration extends JFrame {

/**
 * 
 */
private static final long serialVersionUID = 1L;

private static boolean ranOnce = false;

private JPanel contentPane;
private Registration reg;
private JTextField userTF;
private JTextField passTF;
private JTextField emailTF;

private LoginProcess lp = new LoginProcess();
private JLabel error;
private JButton cancelBtn;

/**
 * Create the frame.
 */
public Registration() {
    if (!ranOnce) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    reg = new Registration();
                    reg.setVisible(true);
                    ranOnce = true;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 245, 212);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(10, 11, 75, 14);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setBounds(10, 36, 75, 14);
    contentPane.add(lblPassword);

    JLabel lblEmail = new JLabel("Email:");
    lblEmail.setBounds(10, 61, 75, 14);
    contentPane.add(lblEmail);

    userTF = new JTextField();
    userTF.setBounds(95, 8, 130, 20);
    contentPane.add(userTF);
    userTF.setColumns(10);

    passTF = new JTextField();
    passTF.setColumns(10);
    passTF.setBounds(95, 33, 130, 20);
    contentPane.add(passTF);

    emailTF = new JTextField();
    emailTF.setColumns(10);
    emailTF.setBounds(95, 58, 130, 20);
    contentPane.add(emailTF);

    error = new JLabel("");
    error.setBounds(10, 154, 215, 14);
    contentPane.add(error);

    JButton regBtn = new JButton("Register");
    regBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            if (!userTF.getText().equals("") || !passTF.getText().equals("") || !emailTF.getText().equals("")) {
                try {
                    if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
// THIS IS LINE 117                         reg.setVisible(false);
                        Main.mainFrame.setVisible(true);
                    } else {
                        if (lp.duplicateAccount) {
                            error.setText("Error: Username already in use.");
                        }
                    }
                } catch (SQLException e) {

                }
            }
        }
    });

    regBtn.setBounds(10, 120, 215, 23);
    contentPane.add(regBtn);

    cancelBtn = new JButton("Cancel");
    cancelBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            reg.setVisible(false);
            Main.mainFrame.setVisible(true);
        }
    });
    cancelBtn.setBounds(10, 86, 215, 23);
    contentPane.add(cancelBtn);

}

}

这是异常错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at frontend.Registration$3.actionPerformed(Registration.java:117)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)

我真的不知道为什么它说我的窗口是空的,什么时候不是。它必须是我在初始化窗口时所做的事情。

2 个答案:

答案 0 :(得分:2)

为什么不起作用?

您没有正确使用 Singleton 设计模式。您是否注意到有两个JFrame弹出?

if (!ranOnce)
    {
        EventQueue.invokeLater(new Runnable()//this call the run method in a separate thread
        {
            public void run()
            {
                try
                {
                    // for the current instance of Registration, you are setting
                    // the reg variable by calling another instance of Registration => thats not the correct way to do a Singleton
                    reg = new Registration(); 
                    reg.setVisible(true);
                    ranOnce = true;
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        });
    }

所以,假设你在main方法中调用new Registration(),你将进入public Registration()构造函数(让我们称之为instance1)并通过再次调用来设置变量reg构造函数new Registration()(instance2)但到目前为止,ranOnce将为true,因此EventQueue.invokeLater将不会被调用=>不会在instance2 =>中设置reg点击第二帧中的NullPointerException(顶部的那个)(instance2)时Jbutton。单击第一帧(instance1)按钮将隐藏第二帧(instance2)。

如何解决?

使用正确的 Singleton

private static final long   serialVersionUID    = 1L;

private static boolean      ranOnce             = false;

private JPanel              contentPane;
private static Registration     reg;
private JTextField          userTF;
private JTextField          passTF;
private JTextField          emailTF;
private LoginProcess lp = new LoginProcess();
private JLabel              error;
private JButton             cancelBtn;




public static synchronized Registration getRegistration()
{
    if(reg == null)
        reg = new Registration();

    return reg;


}

/**
 * Create the frame.
 */
private Registration()
{


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 245, 212);
    setLocationRelativeTo(null);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblUsername = new JLabel("Username:");
    lblUsername.setBounds(10, 11, 75, 14);
    contentPane.add(lblUsername);

    JLabel lblPassword = new JLabel("Password:");
    lblPassword.setBounds(10, 36, 75, 14);
    contentPane.add(lblPassword);

    JLabel lblEmail = new JLabel("Email:");
    lblEmail.setBounds(10, 61, 75, 14);
    contentPane.add(lblEmail);

    userTF = new JTextField();
    userTF.setBounds(95, 8, 130, 20);
    contentPane.add(userTF);
    userTF.setColumns(10);

    passTF = new JTextField();
    passTF.setColumns(10);
    passTF.setBounds(95, 33, 130, 20);
    contentPane.add(passTF);

    emailTF = new JTextField();
    emailTF.setColumns(10);
    emailTF.setBounds(95, 58, 130, 20);
    contentPane.add(emailTF);

    error = new JLabel("");
    error.setBounds(10, 154, 215, 14);
    contentPane.add(error);

    JButton regBtn = new JButton("Register");
     regBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                if (!userTF.getText().equals("") || !passTF.getText().equals("") || !emailTF.getText().equals("")) {
                    try {
                        if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
                        setVisible(false);
                            Main.mainFrame.setVisible(true);
                        } else {
                            if (lp.duplicateAccount) {
                                error.setText("Error: Username already in use.");
                            }
                        }
                    } catch (SQLException e) {

                    }
                }
            }
        });

    regBtn.setBounds(10, 120, 215, 23);
    contentPane.add(regBtn);

    cancelBtn = new JButton("Cancel");
    cancelBtn.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
             setVisible(false);

        }
    });
    cancelBtn.setBounds(10, 86, 215, 23);
    contentPane.add(cancelBtn);



}

public static void main(String[] args)
{
    Registration registration = Registration.getRegistration();
    registration.setVisible(true);
}

答案 1 :(得分:0)

尝试使用setVisible(fasle)代替reg.setVisible(false)