Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

syntax error using constructor

Status
Not open for further replies.

gemann

Technical User
May 21, 2002
9
CA
Hi, I'm getting a syntax error when I'm trying to instansiate a card object with this constructor:

Card card = new Card(((Card)card).value, ((Card)card).suit);

C:\java\War>javac TryDeal.java
TryDeal.java:13: variable card might not have been initialized
Card card = new Card(((Card)card).value, ((Card)card).suit);
^
1 error

Here are my classes :

class TryDeal
{
public static void main(String[] args)
{
Card card = new Card(((Card)card).value, ((Card) card).suit);
CardDeck deck = new CardDeck();
deck.shuffle();

Hand myHand = deck.dealHand(5);
Hand yourHand = deck.dealHand(5);


Card myCard = (Card)myHand.getCard();
Card yourCard = (Card)yourHand.getCard();

Hand returnHand = myCard.addCardBackInHand(card);

System.out.println("\nYour hand is"+yourCard);
System.out.println("\nMy hand is"+myCard);
System.out.println("\nMy return hand is"+returnHand);
}
}

class Card extends Hand implements Comparable
{

public Card(int value, int suit) throws IllegalArgumentException
{
if(value >= ACE && value <= KING)
this.value = value;
else
throw new IllegalArgumentException(&quot;Invalid card value&quot;);
if(suit >= HEARTS && suit <= SPADES)
this.suit = suit;
else
throw new IllegalArgumentException(&quot;Invalid suit&quot;);
}

public int compareTo(Object card)
{
if(this.suit != ((Card)card).suit)
return suit < ((Card)card).suit ? -1: 1;
else
if(this.value == ((Card)card).value)
return 0;
else
return value < ((Card)card).value ? -1 : 1;
}

public Card addCardBackInHand(Card card)
{
return (Card)hand.push(card);
}

public String toString()
{
String cardStr;
switch(value)
{
case ACE: cardStr = &quot;A&quot;;
break;
case JACK: cardStr = &quot;J&quot;;
break;
case QUEEN: cardStr = &quot;Q&quot;;
break;
case KING: cardStr = &quot;K&quot;;
break;
default: cardStr = Integer.toString(value);
break;
}

switch(suit)
{
case CLUBS: cardStr += &quot;C&quot;;
break;
case DIAMONDS: cardStr += &quot;D&quot;;
break;
case HEARTS: cardStr += &quot;H&quot;;
break;
case SPADES: cardStr += &quot;S&quot;;
break;
}
return cardStr;
}


// Suit values
public static final int HEARTS = 0;
public static final int CLUBS = 1;
public static final int DIAMONDS = 2;
public static final int SPADES = 3;

// Card face values
public static final int ACE = 1;
public static final int JACK = 11;
public static final int KING = 12;
public static final int QUEEN = 13;

protected int suit;
protected int value;
}

I'm kind of lost on what to do next. Any help will be greatly appreciated....gemann
 
When you type:
class TryDeal
{
public static void main(String[] args)
{
Card card = new Card(((Card)card).value, ((Card) card).suit);

where did the card variable come from?
 
Well your object card is not initialized, and you are trying to send it null parameters since the object named card is not initialized. You must create the object, before you can access it's properties. Basically you need to send valid parameters to your constructor such as Card card = new Card(8, 2); With your code it would be something like this:
Card is Null, therefore it's properties, suit and value are Null as well. Hopefully that helps.

JavaDude32
 
Thanks JavaDude32 I'm a newbie and I was just taking a shot in the dark with that one. Sorry for the mistakes...gemann
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top