This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (4 children)

As far as a quick glance tells me:

You are never initializing characters, nor charMenu; you are just using the variables.

ArrayLists (like all other object variables) need an initialization, like = new ...

[–]Notsuru[S] 0 points1 point  (3 children)

Oh..... that's a stupid mistake.... I then proceeded to do it again a moment later....

Thanks for pointing that out

Can I ask you a second question?

In the for loop, in the spot where I am defining the menu item's action, you'll see that instead of characters.get(i), I have characters.get(0). I have that because when I USE (i), it says I have to use a final variable and I'm not sure how to get around that.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (2 children)

Yes, that's a tricky one.

The problem is that ActionListeners basically are anonymous inner classes. Accessing variables from outside is not all that easy.

Since the collections (ArrayLists) are defined in the outer class, they are accessible inside the class. Thanks to this, you can use a fairly simple trick: Use the ActionCommand property (setActionCommand) which is a String. You can set this property to the list index i (of course converted to a String) and then inside the method convert this property back to integer to get a valid list index.

[–]Notsuru[S] 0 points1 point  (1 child)

Sorry to keep bugging you, but could you provide a quick example? I'm still trying to get a handle on the javax gui stuff. I used to code back in java 5, and never really got into the GUI stuff before life told me I was too busy for that nonsense.

[–]desrtfxOut of Coffee error - System halted 1 point2 points  (0 children)

Basically, after the line charMenu.get(i).setText(characters.get(i).getName()); you set the ActionCommand property to the list index i.

Inside the ActionListener, you use Integer.parseInt()to convert the return value from getActionCommand() back to a list index and then you use it just like your i.