Could someone look at this error.

Here are the errors:

Compiling 1 source file to C:\Documents and Settings\James Kovacs\JavaGin\build\classes

C:\Documents and Settings\James Kovacs\JavaGin\src\App.java:76: local variable playerHand is accessed from within innerclass; needs to be declaredfinal

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile));//should add card from discard to hand

C:\Documents and Settings\James Kovacs\JavaGin\src\App.java:76: local variable playerHand is accessed from within innerclass; needs to be declaredfinal

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile));//should add card from discard to hand

C:\Documents and Settings\James Kovacs\JavaGin\src\App.java:76: local variable _discardPile is accessed from within innerclass; needs to be declaredfinal

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile));//should add card from discard to hand

C:\Documents and Settings\James Kovacs\JavaGin\src\App.java:77: local variable _discardPile is accessed from within innerclass; needs to be declaredfinal

_discardPile = DrawCardDiscardChange(_discardPile);//take top card from discard

C:\Documents and Settings\James Kovacs\JavaGin\src\App.java:77: local variable _discardPile is accessed from within innerclass; needs to be declaredfinal

_discardPile = DrawCardDiscardChange(_discardPile);//take top card from discard

5 errors

BUILD FAILED (total time: 0 seconds)

Here is the code:

publicclass Appextends javax.swing.JFrame{

/** Creates new form App */

public App(){

initComponents();//creates original state of GUI

getContentPane().setBackground(new Color(0,102,0));//paints the ContentPane to match the GUI

int[] _suitDeck ={0,//card number in array corresponds to suit number

1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,//1 = clubs, 2 = spades, 3 = hearts, 4 = diamonds

1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4,

1,2,3,4,1,2,3,4,1,2,3,4};

int[] _cardDeck = GenerateDeck();//generates an initial deck

String[] CONVERTED_cardDeck = ConvertDeck(_cardDeck);//creates a deck where cards are named

System.out.println("*****ORIGINAL DECK*****");

for (int j=1; j<_cardDeck.length; j++){System.out.println(j +". " + CONVERTED_cardDeck[j]);}//print the original deck

int[] playerHand = DrawHand(_cardDeck);//draws a hand for the player

_cardDeck = DrawHandDeckChange(_cardDeck);//changes deck by taking out player hand

CONVERTED_cardDeck = ConvertDeck(_cardDeck);//updates named deck to print

System.out.println("*****FIRST NEW DECK*****");

for (int j=1; j<_cardDeck.length; j++){System.out.println(j +". " + CONVERTED_cardDeck[j]);}//prints named deck

int[] compHand = DrawHand(_cardDeck);//draws a hand for the computer

_cardDeck = DrawHandDeckChange(_cardDeck);//changes deck by taking out computer hand

CONVERTED_cardDeck = ConvertDeck(_cardDeck);//updates named deck to print

System.out.println("*****SECOND NEW DECK*****");

for (int j=1; j<_cardDeck.length; j++){System.out.println(j +". " + CONVERTED_cardDeck[j]);}//prints named deck

String[] CONVERTED_playerHand = ConvertDeck(playerHand);//creates a named player hand

String[] CONVERTED_compHand = ConvertDeck(compHand);//creates a named computer hand

System.out.println("*****PLAYER HAND*****");

for (int j=1; j<playerHand.length; j++){System.out.println(j +". " + CONVERTED_playerHand[j]);}//prints player hand

DisplayHand(playerHand, jButton1, jButton2, jButton3, jButton4, jButton5, jButton6, jButton7, jButton8);//sets button icons of player hand

System.out.println("*****COMPUTER HAND*****");

for (int j=1; j><compHand.length; j++){System.out.println(j +". " + CONVERTED_compHand[j]);}//prints computer hand

jButton10.setIcon(new javax.swing.ImageIcon("C:\\Documents and Settings\\James Kovacs\\JavaGin\\" + DrawCardDeck(_cardDeck) + "_big.gif")); //sets button icon for discard pile with top card from deck

int[] _discardPile =newint[2];//creats discard pile array

_discardPile[1] = DrawCardDeck(_cardDeck);//adds top card from deck to discard pile

_cardDeck = DrawCardDeckChange(_cardDeck);//updates deck by taking top card

CONVERTED_cardDeck = ConvertDeck(_cardDeck);//updates nameds deck

System.out.println("*****DRAWN FROM DECK*****");

for (int j=1; j><_cardDeck.length; j++){System.out.println(j +". " + CONVERTED_cardDeck[j]);}//prints drawfrom deck

jButton10.addActionListener(new ActionListener( ){

publicvoid actionPerformed(ActionEvent ev){

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile));//should add card from discard to hand

_discardPile = DrawCardDiscardChange(_discardPile);//take top card from discard

}

});

}

[8180 byte] By [jdk2006a] at [2007-11-27 4:19:35]
# 1

Anonymous inner classes require final variables because of the way they

are implemented in Java. An anonymous inner class (AIC) uses local

variables by creating a private instance field which holds a copy of

the value of the local variable. The inner class isn't actually using

the local variable, but a copy. It should be fairly obvious at this

point that a "Bad Thing"?can happen if either the original value or

the copied value changes; there will be some unexpected data synchronization

problems. In order to prevent this kind of problem, Java requires you to

mark local variables that will be used by the AIC as final (i.e.,

unchangeable). This guarantees that the inner class' copies of local

variables will always match the actual values.

yawmarka at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 2
But if I make them final I won't be able to change them like I want to, right? I guess I am looking for a way to right this where I can use the button press (actionperformed) to change them.
jdk2006a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 3

> But if I make them final I won't be able to change

> them like I want to, right? I guess I am looking for

> a way to right this where I can use the button press

> (actionperformed) to change them.

You'll have to either pass them as parameters then, or not use an anonymous inner class.

jverda at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 4

final != immutable

The final keyword does not neccessarily mean you cannot modify the object referenced (for non-primitive) types, it just means that you cannot reuse the variable, i.e.:// it is not possible to reuse a final variable

final List list = new ArrayList();

list = new LinkedList(); // will not compile

// it is ok to modify the object referenced by a final variable

final Map map = new HashMap();

map.put("key", "value");

thomas.behra at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 5
I cant pass them as parameters into the actionperformed class though. And I don't see how to get around the inner class. Could someone offer a suggestion?
jdk2006a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 6

Modify your actionPerformed in the inner class to call a method like doJButton10Action().

Then implement that in your outer class.

jButton10.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

doAction();

}

});

private void doAction(){

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile)); //should add card from discard to hand

_discardPile = DrawCardDiscardChange(_discardPile); //take top card from discard

}

cimmerian76a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 7
that won't actually change the variables though because they are within the function right?
jdk2006a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 8

> that won't actually change the variables though

> because they are within the function right?

That won't even compile, since playerHand and _discardPile are local variables and not fields.

So, either make playerHand and _discardPile member variables (fields) or introduce a data stuct, i.e.

public class App extends javax.swing.JFrame {

/** Creates new form App */

public App() {

// [snip]

final DataHolder data = new DataHolder();

data.hand = playerHand;

data.pile = _discardPile;

jButton10.addActionListener(new ActionListener( ) {

public void actionPerformed(ActionEvent ev) {

data.hand = DrawCardHandChange(data.hand, DrawCardDiscard(data.pile));

data.pile = DrawCardDiscardChange(data.pile);

}

});

}

private static final class DataHolder {

int[] hand;

int[] pile;

}

}

With this approach you will obviously have to adjust the code that accesses playerHand or _discardPile to access data.hand and data.pile respectively.

thomas.behra at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 9

> that won't actually change the variables though

> because they are within the function right?

I'm not entirely sure what you are asking here, but all listener.actionPerformed(..) is doing is calling App.doAction()

which in turn operates on App.playerHand and App.discardPile as (I assume) you intended.

cimmerian76a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 10

> > that won't actually change the variables though

> > because they are within the function right?

>

> I'm not entirely sure what you are asking here, but

> all listener.actionPerformed(..) is doing is calling

> App.doAction()

> which in turn operates on App.playerHand and

> App.discardPile as (I assume) you intended.

Well, I could be mistaken, but I always thought thatpublic class App extends javax.swing.JFrame {

/** Creates new form App */

public App() {

// [snip]

int[] playerHand = DrawHand(_cardDeck); //draws a hand for the player

// [snip]

int[] _discardPile = new int[2];

// [snip]

}

}

would result in playerHand and _discardPile to be variables local to the ctor of class App. Might be that you cannot reference them in a non-args method for this reason ...

thomas.behra at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...
# 11

My bad... I wasn't specific about making those arrays fields in the class.

public class App extends javax.swing.JFrame {

/** Creates new form App */

int[] playerHand;

int[] _discardPile;

JButton jButton10;

//And so on...

public App() {

//initializing the card piles, etc...

jButton10.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent ev) {

doAction();

}

});

}

private void doAction(){

playerHand = DrawCardHandChange(playerHand, DrawCardDiscard(_discardPile)); //should add card from discard to hand

_discardPile = DrawCardDiscardChange(_discardPile); //take top card from discard

}

}

cimmerian76a at 2007-7-12 9:26:32 > top of Java-index,Java Essentials,Java Programming...