2

Errors When i Compile the file I get no errors but when I try to execute this I get the following messages:

Exception in thread "main" java.lang.StackOverflowError
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1988)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1997)
at sun.awt.SunToolkit.isInstanceOf(SunToolkit.java:1982)
at javax.swing.LookAndFeel.installProperty(LookAndFeel.java:275)
at javax.swing.plaf.basic.BasicButtonUI.installDefaults(BasicButtonUI.java:102)
at javax.swing.plaf.metal.MetalButtonUI.installDefaults(MetalButtonUI.java:80)
at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:88)
at javax.swing.JComponent.setUI(JComponent.java:664)
at javax.swing.AbstractButton.setUI(AbstractButton.java:1807)
at javax.swing.JButton.updateUI(JButton.java:146)
at javax.swing.AbstractButton.init(AbstractButton.java:2172)
at javax.swing.JButton.<init>(JButton.java:136)
at javax.swing.JButton.<init>(JButton.java:109)
at Paneel$boven.<init>(Loterij3.java:65)
at Paneel$kies.<init>(Loterij3.java:125)
at Paneel$boven.<init>(Loterij3.java:66)
at Paneel$kies.<init>(Loterij3.java:125)

And then the last lines just repeats themselves many times.

Question: What do I have to change in my code to make it work?

Note: I'm new to Java and I know there are more posts like this one but I just can't apply them to my code with my current limited understanding of Java.

If someone wants to know: I'm using JCreator.

What i'm trying to make: What I'm trying to make is fairly simple.

1) Fill in a name in the JTextField, press enter and the name should appear in the JTextArea. After the name is in the JTextArea the JTextField becomes empty so you can fill another name and so on there should appear a list of names in JTextArea. (this is what I'm now trying to make)

2) Push the button kiesWin to make the program choose a random person from the list.

3) Push the button resetL to reset the program so I can make a new list to choose a random winner from it.

The whole code:

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

// Main method to make the frame
public class Loterij3 extends JFrame {
public static void main( String args[] ) {
    JFrame frame = new Loterij3();
    frame.setExtendedState( frame.MAXIMIZED_BOTH );
    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    frame.setTitle( "Klanten Register" );
    frame.setContentPane( new Paneel() );
    frame.setVisible( true );
}
}

class Paneel extends JPanel {
private boven Boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();

public Paneel() {
setLayout( new BorderLayout() ); // using border Layout.
setBackground( Color.LIGHT_GRAY );

textvak1 = new JTextArea();
add( new JScrollPane( textvak1 ) );
textvak1.setBackground( Color.WHITE );

textvak2 = new JTextField();
textvak2.setHorizontalAlignment(JTextField.CENTER);
textvak2.setEditable( false );

Boven = new boven();    

add( Boven, BorderLayout.NORTH );
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}

class boven extends JPanel {
JButton kiesWin, resetL;
JLabel label1;
JTextField invoervak1;

public boven() {
    setBackground( Color.LIGHT_GRAY );
    setLayout( new GridLayout( 1, 4, 100, 5 ) ); // using GridLayout.
    Border border = 
        BorderFactory.createEmptyBorder( 10, 10, 10, 10 );
    setBorder( border );

    kiesWin = new JButton("Kies een Winnaar!");
    kiesWin.addActionListener( new kies() );
    resetL = new JButton("Reset alles");
    resetL.addActionListener( new reset() );
    label1 = new JLabel("Voer Persoon in en druk op enter: ", JLabel.RIGHT);
    invoervak1 = new JTextField( 20 );
    invoervak1.addActionListener( new InvoerVakHandler() );

    add( label1 );
    add( invoervak1 );
    add( kiesWin );
    add( resetL );
    }
}

// de naam
class naam {
    private String ingevoerdNaam;

    public naam( String ingevoerdNaam) {
        this.ingevoerdNaam = ingevoerdNaam;
    }

    public String getIngevoerdNaam() {
        return ingevoerdNaam;
    }
}

// Arraylist
class OnthoudNaam extends JPanel {
    private ArrayList<naam> lijst;

    public OnthoudNaam() {
        lijst = new ArrayList<naam>();
        }

        public void voegNaamToe(naam x ) {
        lijst.add(x);
        }

        public String toString() {
        StringBuffer buffer = new StringBuffer();
        for(naam x : lijst ) {
        buffer.append( x );
        buffer.append( "\n" );
    }
    return buffer.toString();
}
}

// invoer handler
public class InvoerVakHandler extends boven implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        String invoer = invoervak1.getText();
        naam naam = new naam( invoer );
        onthoudNaam.voegNaamToe( naam );
        textvak1.setText( onthoudNaam.toString() );
    }
}
    // kies
class kies extends boven implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}

// reset
class reset extends boven implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}
}

For everyone who is trying to help me: Thank you for all your help and patience in advance!

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Morganis
  • 652
  • 5
  • 13
  • 29
  • possible duplicate of [What is a stack overflow error?](http://stackoverflow.com/questions/214741/what-is-a-stack-overflow-error) –  Mar 27 '14 at 05:45

5 Answers5

4

In the constructor of boven, you call

kiesWin.addActionListener( new kies() );

the constructor of kies, but

class kies extends boven implements ActionListener {

that constructor calls the constructor of boven. You have an infinite recursion.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
2

A StackOverflowError means that you have too many function calls to fit their data on the stack. Usually it's an indication that you have infinite recursion going on, as you do in this case.

When you create a "boven," it creates a new "kies." But kies extends boven, so you wind up creating a boven which creates a kies which creates a boven which creates a kies which eventually blows up the stack.

To fix this, kies should probably not be a boven.

Jon S.
  • 1,378
  • 8
  • 14
2

This is the problem:

// Constructor for the boven class
public boven() {
    ...
    kiesWin.addActionListener( new kies() );
    ...
}

...
class kies extends boven {
}

So to create an instance, of boven, you have to create an instance of kies... but as kies extends boven, creating an instance of kies ends up back in the constructor of boven, which creates another instance of kies etc.

It's not clear why kies extends boven in the first place - I suspect it shouldn't.

It's not actually clear to me what your code is trying to achieve (it's hard to read, so I haven't looked in too much detail) but I have general suggestions:

  • Start following Java naming conventions (capitalize your class names, for example)
  • Avoid nested classes - generally prefer create top level classes
  • Avoid doing so much work in constructors
  • Prefer composition to inheritance
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

It's quite simple really.

To construct an instance of boven, you need to construct an instance of kies to act as the action listener for the "Kies een Winnaar" button.

However, kies is a subclass of boven, and so it involves the same constructor. So to contruct a boven, you need to construct a boven, which means constructing a boven, which means constructing a boven...

The only way to fix this is to break the infinite cycle. A boven object cannot rely on another instance existing in its only constructor, else it's impossible to create. It seems like your class hierarchy may be wrong, since it doesn't like an action listener would need to be the same class that is creating the buttons in the first place. If you just remove the subclass relationship, I believe things would work as you intended.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
0

kies is a subclass of boven. So, when you are creating a new kies, which is what this indicates:

Paneel$kies.<init>

the constructor of boven gets run. boven's constructor creates a new kies. So you get an infinite loop.

Rick Barkhouse
  • 1,186
  • 2
  • 10
  • 15