0

Afternoon all, i have a problem with some JFrame code, this JFrame is started when the user presses "New User", and whenever they do so, i get:

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)

With this code:

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);


}
}

I know this error is caused by an infinite loop, or something. but i have a boolean in place to stop it from infinitely running. This code was working about 20mins ago, and i haven't changed the first part of the constructor since i created the code.

Any ideas? Thanks..

joelwmale
  • 121
  • 1
  • 3
  • 10
  • 1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Sep 24 '14 at 06:13

2 Answers2

3

You create a new Object in the line private Registration reg = new Registration();.

You should create this object in the consturctor.

Jens
  • 67,715
  • 15
  • 98
  • 113
1

The problem at that line:

private Registration reg = new Registration();

You get in a infinite loop and then stack overflow.

DiogoSantana
  • 2,404
  • 2
  • 19
  • 24