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

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
BlitzDodo
  • 27
  • 6
  • is it working when you set ```jt.setText("TestText");```? – Daniel_Kamel Aug 29 '19 at 12:04
  • Nope, he doesn't find jt either :/ – BlitzDodo Aug 29 '19 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"); – ItFreak Aug 29 '19 at 12:08
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – AxelH Aug 29 '19 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. – AxelH Aug 29 '19 at 12:15
  • @ItFreak, u are right with the line54 it is System.out.print() but the Method windowStart() has a full list of Pizza I tested it out, all 5 Pizzas are in windowStart(). – BlitzDodo Aug 29 '19 at 12:16
  • Sry forgott to Mention it, my button works fine when I just use a System.out.print("test") and press the button, I got a Output in the console with "test". When I just try jt.setText("Test") I get a java.lang.NullPointerException. And it is – BlitzDodo Aug 29 '19 at 12:19

1 Answers1

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);
AxelH
  • 14,325
  • 2
  • 25
  • 55