0

I am doing this programming assignment for uni and I am using an ArrayList, now when I select a button I want a textfield to display the farm name text from that ArrayList. Here is a screenshot of my code and errors. The code for jtfFarmName.setText.... works out of "display(int i)" but when I put that code in "display(int i)" and call it like this "display(0)" it gives me that error. enter image description here Any help would be amazing, thanks. It is really annoying me. I have already initialized the farm variable; enter image description here

Robdogga55
  • 309
  • 1
  • 4
  • 12
  • According to your update, you've only set up the label called "farms" but you aren't actually initializing it to anything. When the program loads, unless you have a `farms = new ....` line somewhere else before that display method, `farms` is still null. – Paul Richter Oct 16 '15 at 19:47
  • I am creating it, just a little further down, I really should have specififed that.. `// Create farms ArrayList farms = new ArrayList(); farms.add(new Farm("Sunny Side Farms", "Ballarat", 2, 3, 5, 1)); farms.add(new Farm("Sunny Side Farms", "Ballarat", 2, 3, 5, 1)); farms.add(new Farm("Sunny Side Farms", "Ballarat", 2, 3, 5, 1));` – Robdogga55 Oct 16 '15 at 19:53
  • 1
    Ok, so by the looks of it, you're actually defining a **local** variable, because you specified the type again (the `ArrayList` part in front of the label `farms`). In order to set the **instance** variable called `farms`, you'll need to drop that type definition, and (sometimes optional but HIGHLY recommended regardless) reference it by using `this.farms` instead. – Paul Richter Oct 16 '15 at 19:56
  • Also note that code in the comments doesn't format properly as you can see. If that last note didn't solve the problem, it would probably be better to simply edit the question and add the code there. – Paul Richter Oct 16 '15 at 19:57
  • @PaulRichter Thanks so much for your help. I will put the solution in the question area above. Now I just have to finish this program by next Friday. – Robdogga55 Oct 16 '15 at 20:07
  • Plenty 'o time. Good luck! – Paul Richter Oct 16 '15 at 20:08

0 Answers0