Object reference not set to an instance of an object.

  • Thread starter Thread starter Tommy Lang
  • Start date Start date
T

Tommy Lang

Why doesn't the following code work?
I get an error at the following line...
CardGroup[0].PropertyCardType = (CardType)0;
The error is "Object reference not set to an instance of an object.", which
means the object in question have not been initiated.
But I have init the object above n this line...
public Card [] CardGroup = new Card[53];

Anybody knows how to resolve this??

Thanks :-)

namespace CardLibrary
{
public class CardFactory
{
//Create and init CardGroup
public Card [] CardGroup = new Card[53];

//Construktor
public CardFactory()
{
CardGroup[0].PropertyCardType = (CardType)0;

Console.WriteLine("Konstruktor: public CardFactory()");
}
} //Slut public class CardFactory
} //Slut namespace CardLibrary
 
You're trying to convert an integer to a CardType object, I would have thought it would give some sort of type mismatch error. Maybe
CardGroup[0] is null.
 
I'm inferring this but it looks like Card is a reference type i.e. a class

So public Card [] CardGroup = new Card[53];
will create an array that holds Card instances, but not create those
instances.

You have to loop through that array and create them:
for(int c=0;c<CardGroup.Length;c++)
CardGroup[c]=new Card();

Then CardGroup[0].PropertyCardType = (CardType)0; should work.

Richard
 
You're trying to convert an integer to a CardType object, I would have thought it would give some sort of type mismatch error.

My mistake, I presume CardType is an enum.

--
Michael Culley


Michael Culley said:
You're trying to convert an integer to a CardType object, I would have thought it would give some sort of type mismatch error. Maybe
CardGroup[0] is null.

--
Michael Culley


Tommy Lang said:
Why doesn't the following code work?
I get an error at the following line...
CardGroup[0].PropertyCardType = (CardType)0;
The error is "Object reference not set to an instance of an object.", which
means the object in question have not been initiated.
But I have init the object above n this line...
public Card [] CardGroup = new Card[53];

Anybody knows how to resolve this??

Thanks :-)

namespace CardLibrary
{
public class CardFactory
{
//Create and init CardGroup
public Card [] CardGroup = new Card[53];

//Construktor
public CardFactory()
{
CardGroup[0].PropertyCardType = (CardType)0;

Console.WriteLine("Konstruktor: public CardFactory()");
}
} //Slut public class CardFactory
} //Slut namespace CardLibrary
 
This solved my problem. I had created an array that holds Card
instances, but not created the instances themself. Card is a class and
CardType is an Enum.

Thanks,
Tommy




Richard A. Lowe said:
I'm inferring this but it looks like Card is a reference type i.e. a class

So public Card [] CardGroup = new Card[53];
will create an array that holds Card instances, but not create those
instances.

You have to loop through that array and create them:
for(int c=0;c<CardGroup.Length;c++)
CardGroup[c]=new Card();

Then CardGroup[0].PropertyCardType = (CardType)0; should work.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Tommy Lang said:
Why doesn't the following code work?
I get an error at the following line...
CardGroup[0].PropertyCardType = (CardType)0;
The error is "Object reference not set to an instance of an object.", which
means the object in question have not been initiated.
But I have init the object above n this line...
public Card [] CardGroup = new Card[53];

Anybody knows how to resolve this??

Thanks :-)

namespace CardLibrary
{
public class CardFactory
{
//Create and init CardGroup
public Card [] CardGroup = new Card[53];

//Construktor
public CardFactory()
{
CardGroup[0].PropertyCardType = (CardType)0;

Console.WriteLine("Konstruktor: public CardFactory()");
}
} //Slut public class CardFactory
} //Slut namespace CardLibrary
 
You, sir, are a gentleman! Always a pleasure to help.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Tommy Lang said:
This solved my problem. I had created an array that holds Card
instances, but not created the instances themself. Card is a class and
CardType is an Enum.

Thanks,
Tommy




"Richard A. Lowe" <[email protected]> wrote in message
I'm inferring this but it looks like Card is a reference type i.e. a class

So public Card [] CardGroup = new Card[53];
will create an array that holds Card instances, but not create those
instances.

You have to loop through that array and create them:
for(int c=0;c<CardGroup.Length;c++)
CardGroup[c]=new Card();

Then CardGroup[0].PropertyCardType = (CardType)0; should work.

Richard

--
C#, .NET and Complex Adaptive Systems:
http://blogs.geekdojo.net/Richard
Tommy Lang said:
Why doesn't the following code work?
I get an error at the following line...
CardGroup[0].PropertyCardType = (CardType)0;
The error is "Object reference not set to an instance of an object.", which
means the object in question have not been initiated.
But I have init the object above n this line...
public Card [] CardGroup = new Card[53];

Anybody knows how to resolve this??

Thanks :-)

namespace CardLibrary
{
public class CardFactory
{
//Create and init CardGroup
public Card [] CardGroup = new Card[53];

//Construktor
public CardFactory()
{
CardGroup[0].PropertyCardType = (CardType)0;

Console.WriteLine("Konstruktor: public CardFactory()");
}
} //Slut public class CardFactory
} //Slut namespace CardLibrary
 
Back
Top