8 Replies - 1855 Views - Last Post: 16 September 2010 - 08:11 AM Rate Topic: -----

#1 blze001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-September 10

TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 01:04 PM

I'm building a Black Jack game and I'm running into an error when trying to make an array of custom objects (Cards)

This is the code that is throwing the issues

    
class Deck
    {
        static Random randomChooser = new Random();
        private Card[] givenDeck;
        private Card[] usedCards;
        private int index = 0;

        public Deck(int[] v, string[] t, string[] s)
        {
            int[] values = v;
            string[] type = t;
            string[] suit = s;
            foreach(string r in suit)
            {
                foreach(string x in type)
                {
                    foreach(int n in values)
                    {
                        Card toAdd = new Card(x, r, n);
                        givenDeck[index] = toAdd;
                        index++;
                    }
                }
            }
        }
    }



For reference, this is what I have for Card.cs so far
    class Card
    {
        private string name;
        private string suit;
        private int value;

        public Card(string t, string s, int v)
        {
            name = t;
            suit = s;
            value = v;
        }
    }



Thanks for any help in advance

EDIT: The arrays that are being passed into the Card constructor are arrays of ints and strings that correspond with important card facts (type, suit, value).

This post has been edited by blze001: 15 September 2010 - 01:05 PM


Is This A Good Question/Topic? 0
  • +

Replies To: TypeInitializationException when constructing an array of objects

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 01:06 PM

Can't you copy and paste the error and point on the line on which it's occurring, so we don't have to work so hard at being psychic?
Was This Post Helpful? 1
  • +
  • -

#3 FlashM   User is offline

  • D.I.C Lover
  • member icon

Reputation: 383
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 01:15 PM

Could you post a code where you initialize your deck?
Was This Post Helpful? 0
  • +
  • -

#4 blze001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-September 10

Re: TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 07:56 PM

Sorry, I forgot to add the marker to the line that threw the error. The error says:

"TypeInitilizationException was unhandled"
"The type initializer for 'BlackJackProject.Program' threw an exception."

The error occurs when this line is executed:
givenDeck[index] = toAdd;


Here is the code that creates the deck:

    
class Program
    {
        static string[] suits = {"Hearts","Spades","Clubs","Diamonds"};
        static string[] types = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
        static int[] values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11 };
        static Deck deckInUse = new Deck(values, types, suits);



It is at the very beginning and executes when the program initializes.

This post has been edited by blze001: 15 September 2010 - 07:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 08:57 PM

OK, so where do you create an instance of givenDeck? You know, like you did with Card right before it?
Was This Post Helpful? 0
  • +
  • -

#6 blze001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-September 10

Re: TypeInitializationException when constructing an array of objects

Posted 15 September 2010 - 09:14 PM

It is initializing within the Deck.cs object class. I have one Deck object that contains an array of Card objects and handles the drawing of said cards.

    
class Deck
    {
        static Random randomChooser = new Random();
        private Card[] givenDeck;
        private Card[] usedCards;
        private int index = 0;



I tried initializing with:

        private Card[] givenDeck = new Card[52];



but no cigar.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: TypeInitializationException when constructing an array of objects

Posted 16 September 2010 - 04:42 AM

I'm sorry, but what does "but no cigar" mean? We're not standing over your shoulder looking at your screen.

Have you run the code within Visual Studio, so that when it crashes you can check what the values are at the point of failure? You know, debugging 101?

Wrap your code in a try-catch block like this:
try
{    
    Card toAdd = new Card(x, r, n);
    givenDeck[index] = toAdd;
    index++;
} 
catch(TypeInitializationException ex)
{
    Console.WriteLine("Exception {0}", ex.Message);
}

Set a breakpoint (F9) on the Console.WriteLine line and run the program in Visual Studio with F5. When the breakpoint is hit you should look at the value of ex.InnerException to get more details on what is causing the exception.
Was This Post Helpful? 1
  • +
  • -

#8 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: TypeInitializationException when constructing an array of objects

Posted 16 September 2010 - 07:51 AM

I found the error, but I'll let you figure out the exact problem and solution. I will give you a hint. JackOfAllTrades is correct, you need to debug. Put the breakpoint on this line in Deck : int[] values = v;
Was This Post Helpful? 0
  • +
  • -

#9 MikeAbyss   User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 56
  • Joined: 09-July 07

Re: TypeInitializationException when constructing an array of objects

Posted 16 September 2010 - 08:11 AM

foreach (string r in suit)
                {
                    foreach (string x in type)
                    {
                        foreach (int n in values)
                        {
                            Card toAdd = new Card(x, r, n);
                            givenDeck[index] = toAdd;
                            index++;
                        }
                    }
                }



private Card[] givenDeck = new Card[52];



should be

private Card[] givenDeck = new Card[676];



Logically this doesn't make sense in a game of cards.

Why do you have values? If you're creating a deck (52 cards) you only need suit and type

Array size of Values = 13, Suit = 4, Type = 13

13*13*4 = 676
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1