簡體   English   中英

AWT-EventQueue-0“ StackOverflowError

[英]AWT-EventQueue-0" StackOverflowError

下午,我在使用某些JFrame代碼時遇到了問題,當用戶按下“新用戶”時,將啟動此JFrame,並且每當他們這樣做時,我都會得到:

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
at frontend.Registration.<init>(Registration.java:36)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)
at frontend.Registration.<init>(Registration.java:25)

使用此代碼:

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 = new Registration();
private JTextField userTF;
private JTextField passTF;
private JTextField emailTF;

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

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

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 245, 195);
    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: Username already in use");
    error.setBounds(10, 120, 215, 14);
    contentPane.add(error);

    JButton regBtn = new JButton("Register");
    regBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
                if (lp.newUser(userTF.getText(), passTF.getText(), emailTF.getText())) {
                    reg.setVisible(false);
                    Main.mainFrame.setVisible(true);
                } else {
                    if (lp.duplicateAccount) {
                        error.setText("lol");
                    }
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    regBtn.setBounds(10, 86, 215, 23);
    contentPane.add(regBtn);


}
}

我知道此錯誤是由無限循環或其他原因引起的。 但是我有一個布爾值來阻止它無限運行。 這段代碼大約在20分鍾前就可以使用了,自從我創建代碼以來,我就沒有更改過構造函數的第一部分。

有任何想法嗎? 謝謝..

您在private Registration reg = new Registration();行中創建一個新對象private Registration reg = new Registration();

您應該在構造器中創建此對象。

那一行的問題是:

private Registration reg = new Registration();

您陷入無限循環,然后堆棧溢出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM