2

I wanted a frame with one TextArea and one Button. When I press the button, the TextArea should show a food menu of 5 Pizzas, well it shows nothing at all, except for the console which shows

"Exception in thread "AWT-EventQueue-0"  
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at de.kvwl.pizza.MainFrame.actionPerformed(MainFrame.java:54)"

In the method windowsStart() the objects exists and are adjustable. In the actionPerformed()Method the objects are … kind of invisible, not existing?

public void windowStart()
{
    MainFrame mFrame = new MainFrame();
    PizzaReader2 test = new PizzaReader2();
    pPizza = test.csvRead();
    
    System.out.println("\n\n\n" + pPizza.get(0) + "\n\n\n");

    f = new JFrame("Textfield");
    b = new JButton("Menu");
    jt = new JTextArea(10,10);
    JPanel pTextArea = new JPanel();
    b.addActionListener(mFrame);

    pTextArea.add(jt);
    pTextArea.add(b);
    f.add(pTextArea);

    f.setSize(300, 300);
    f.setVisible(true);
}
public void actionPerformed(ActionEvent e) 
{    
    //jt.setText("TestText");
    System.out.println("\n\n\n" + pPizza.get(0) + "\n\n\n");
     
    String s = e.getActionCommand(); 
    if (s.equals("Menu")) 
    { 
        System.out.println("Button gedrückt");
        //jt.setText("");
        for (int i = 0; i < pPizza.size(); i++) 
        {
            jt.append(pPizza.get(i)+"\n");
        }

The TextArea should get the value of the ArrayList

7
  • is it working when you set jt.setText("TestText");? Commented Aug 29, 2019 at 12:04
  • Nope, he doesn't find jt either :/ Commented Aug 29, 2019 at 12:06
  • 3
    I dont think that there is a problem with your TextField. IndexOutOfBoundsException looks like your list is empty. Have you tried to print the list to the console? Line 54 could be the line where you call System.out.println("\n\n\n" + pPizza.get(0) + "\n\n\n"); Commented Aug 29, 2019 at 12:08
  • Possible duplicate of What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? Commented Aug 29, 2019 at 12:11
  • Array and List use the same logic in this case. You list is empty, as mentionned in the exception message "Index: 0, Size: 0" when you try to get the value at index 0. Commented Aug 29, 2019 at 12:15

1 Answer 1

3

Your exception occurs in : at de.kvwl.pizza.MainFrame.actionPerformed(MainFrame.java:54)

This action is linked during windowStart with b.addActionListener(mFrame);.

But What I see is that you pass another instance of MainFrame called mFrame as parameter (as an ActionListener). This mFrame never load the list with

pPizza = test.csvRead();

So in short, you have two instance MainFrame:

  • one created and use to call windowStart
  • one created in windowsStart and use to execute actionPerformed.

This last one never load the list of data. Explaining why your list is populated in windowStart but not in actionPerformed, you are actually using two distinct instance MainFrame with two list pPizza.

You can correct this by removing this second instance and use this, the first instance as an ActionListener

b.addActionListener(this);
Sign up to request clarification or add additional context in comments.

1 Comment

That was the Problem, thanks alot. And thanks for the Explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.