0

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)

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 understand them with my current understanding of Java.

I'm trying to solve this for days now (yes days, i'm a complete beginner/noob in java) so please don't give comments like go google it.

And for in case 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.

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!

Morganis
  • 652
  • 5
  • 13
  • 29
  • 2
    You can't access instance stuff from a static context, as the properties won't know what instance to bind to. – alex May 07 '13 at 22:10
  • 6
    the problem is `onthoudNaam.voegNaamToe( naam );` you need to create an instance of class `onthoudNaam` (class should be named OnthoudNaam), and invoke that method on that instance: `OnthoudNaam onthoudNaam = new OnthoudNaam(); (... - later on) onthoudNaam.voegNaamToe( naam );` – dantuch May 07 '13 at 22:12
  • changed my code and the error messages dissaperd. When I compiled it it shows no errors but when I execute it shows the following message: (changed code in main post and posted the errors) – Morganis May 08 '13 at 10:12
  • @DimitriDuvanskij There's little value in editing this existing question, especially as this is effectively a completely different problem. As such, it would probably make sense to open a new question (after checking to see if there's an existing question that addresses this issue). – John Parker May 08 '13 at 10:15

3 Answers3

1

You are making a static reference:

onthoudNaam.voegNaamToe( naam );

You have a class named onthoudNaam, upon which you call are calling a method. You cannot call methods without instanciating an object of the class if they are not defined static. This is why Java is complaining. If you want to use voegNaamToe( naam) to change c variables, you should make it static. If you want to use the method without making it static, you should make an object of the class onthoudNaam.

It is also not advisable to start class names with a lowercase letter. It is good to follow conventions when naming classes, methods and variables:

http://www.oracle.com/technetwork/java/javase/documentation/codeconvtoc-136057.html

Stefan Pante
  • 428
  • 2
  • 12
  • changed my code and the error messages dissaperd. When I compiled it it shows no errors but when I execute it shows the following message: (changed code in main post and posted the errors) – Morganis May 08 '13 at 10:12
0

The problem is in these lines:

onthoudNaam.voegNaamToe( naam );
textvak1.setText( onthoudNaam.toString() );

You're using these methods on onthoudNaam, which is a class. However, the methods you're using are instance methods, so they need to be invoked on an instance of the class. You'll need to do new onthoudNaam() somewhere to create an instance. I'm not 100% sure what your code does, but I think it will work if you put it where you declare your variables:

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

and later:

varName.voegNaamToe( naam );
textvak1.setText(varName.toString() );

Obviously, you'll want to choose a more descriptive name than varName.

nullptr
  • 2,244
  • 1
  • 15
  • 22
  • changed my code and the error messages dissaperd. When I compiled it it shows no errors but when I execute it shows the following message: (changed code in main post and posted the errors) – Morganis May 08 '13 at 10:11
0

You can freely access static fields/methods from non-static fields, but you can't do it vice versa. You can pass non-static variables to static fields as arguments, though.

Below is small example how "static" works:

public class TwoInts {
    int noStatic = 0;
    static int yesStatic = 0;

    //constructor
    public TwoInts(int noStatic, it yesStatic) {
        this.noStatic = noStatic;
        this.yesStatic = yesStatic;
    }
}

We are creating three TwoInts objects: TwoInts obj1 = new TwoInts(10, 5);

Current values:

obj1 noStatic = 10, yesStatic = 5

TwoInts obj2 = new TwoInts(7, 15);

Current values:

obj1 noStatic = 10, yesStatic = 15;
obj2 noStatic = 7, yesStatic = 15;

TwoInts obj3 = new TwoInts(20, 0);

Current values:

obj1 noStatic = 10, yesStatic = 0;
obj2 noStatic = 7, yesStatic = 0;
obj3 noStatic = 20, yesStatic = 0;

I hope this explains what "static" keyword do. ;)

Mac70
  • 320
  • 5
  • 15