Question

Error: Strange enough I get no errors.

What I'm trying to make: ( I also added // messages that explains what my code does.)

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. (here it goes wrong)

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

Problem: When I Push the button Kies (Translated: Choose) it should choose a random name from the ArrayList and show the random name in the JTextField textvak2. But when I push the button Kies the program does nothing. And it should show me the random chosen name from the ArrayList.

This is the class that doesn't works properly: (I Think)

// This is the button that chooses a random name from the ArrayList.
// The random chosen name should appear in the JTextField textvak2. (but it doesn't)
// This is also the part where it goes wrong at the moment.
class Kies extends OnthoudNaam implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        Random r = new Random();
        if (lijst.size() > 0) {
            int n = r.nextInt(lijst.size());
            Naam kiesNaam = lijst.get(n);
            textvak2.setText(kiesNaam.getIngevoerdNaam());
            }
    }
}

For in case you need 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( "My Lottery!" );
    frame.setContentPane( new Paneel() );
    frame.setVisible( true );
}
}

// This is the Panel that goes into the frame.
class Paneel extends JPanel {
private Boven boven;
JTextArea textvak1;
JTextField textvak2;
OnthoudNaam onthoudNaam = new OnthoudNaam();
JTextField invoervak1; // JTextField from class Boven.

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

boven = new Boven(); 

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

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

add( boven, BorderLayout.NORTH ); // Where the class Boven should be.
add( textvak1, BorderLayout.CENTER );
add( textvak2, BorderLayout.SOUTH );
}

// This is the class Boven (Translation up or upper).
// This is where the JButtons, JTextField and JLabels are.
public class Boven extends JPanel {
JButton kiesWin, resetL;
JLabel label1;

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

// The class Naam (translation = name).
// This is what the ArrayList should remember
// In other words ArrayList should remember the Names I put in the JTextField.
class Naam {
    private String ingevoerdNaam;

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

    public String getIngevoerdNaam() {
        return ingevoerdNaam;
    }

    public String toString() {
        return ingevoerdNaam;
    }
}

// This is my ArrayList,
// This should remember the names I type in the JTextField.
class OnthoudNaam extends JPanel {
    protected 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();
}
}

// This is the JTextField where I enter the names.
// The Name I fill in the JTextField should be remembered by the ArrayList.
// The Name I fill in should be put in the JTextArea.
public class InvoerVakHandler implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        String invoer = invoervak1.getText();
        Naam Naam = new Naam( invoer );
        onthoudNaam.voegNaamToe( Naam );
        textvak1.setText( onthoudNaam.toString() );
        invoervak1.setText( "" );
    }
}
    // This is the button that chooses a random name from the ArrayList.
    // This is also the part where it goes wrong at the moment.
class Kies extends OnthoudNaam implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
        Random r = new Random();
        if (lijst.size() > 0) {
            int n = r.nextInt(lijst.size());
            Naam kiesNaam = lijst.get(n);
            textvak2.setText(kiesNaam.getIngevoerdNaam());
            }
    }
}

// This should become the button that resets everything so you can start over.
class Reset implements ActionListener {
    public void actionPerformed( ActionEvent e ) {
    }
}
}
Was it helpful?

Solution

Random#nextInt requires a positive number but the initial size of the List lijst is 0 hence the Exception

The docs clearly state this

Throws: IllegalArgumentException - if n is not positive

Check that there are entries in the List first.

if (lijst.size() > 0) {
   int n = r.nextInt(lijst.size());
   Naam kiesNaam = lijst.get(n);
   textvak2.setText(kiesNaam.getIngevoerdNaam());
}

Aside: Rather than casting the object from the List, extract the Naam object and use its getIngevoerdNaam method.

Remember too The debugger is your friend

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top