3 Replies Latest reply: Mar 18, 2009 12:32 PM by 807588 RSS

    Objects stored in an array return null when toString method is used.  Help?

    807588
      I'm creating a simplistic, text based blackjack program. I have a Card class that I use to create card objects that are stored in an array. The cards have basic stats used by the computer for determining the path of the game, but I also have Strings variables in the card object for my toString method. The method takes the String for the number (i.e one, two, jack, ace) called numberstring and one for the suit called suit. In my driver I have created card objects and assigned them values, but when I use the toString method, I get "null of nulls" instead of Ace of Spades or something along those lines.

      Here is my card class containing the methods mentioned.
      public class Card
      {

      private String numberstring, suit;
      private int number, value;
      public boolean dealer, player, indeck;

           public Card(int number_, int value_, String numberstring_, String suit_, boolean player_, boolean dealer_, boolean indeck_)
           {
                int number = number_;
                int value = value_;
                String numberstring = numberstring_;
                String suit = suit_;
                boolean player = player_;
                boolean dealer = dealer_;
                boolean indeck = indeck_;
           }

      public String toString()
           {
           String faceName;
                faceName = ((this.numberstring) + " of " + (this.suit) + "s");
                System.out.println(faceName);
      return faceName;
           }

           public String getnumberstring(card parameter)
           {
                System.out.print(this.numberstring);
                return numberstring;
           }
           public String getsuit(Card parameter)
           {
                return suit;
           }

      And the troublesome lines of code from my driver.
      Card SpadeA = new Card(1, 12 ,"Ace" ,"Spade" ,false ,false ,true);
      SpadeA.toString();
      The 12 is just a note to tell me where in the array it is. and the booleans are for a different part of the program.

      I know forums like this often get high school students in Java classes just looking for easy answers without doing the work. But up until now I have programmed this myself and just need to know how to get my methods to return and print the values correctly. Thank you.