0

I'm trying to create a class object array like this for "CruiseServerHttpClient" class

private CruiseServerHttpClient[] _cruiseManager;

for (int i=0;i<2;i++)
{
_cruiseManager[i]=new CruiseServerHttpClient();
}

Is this the right way to create the object. I get Null reference exception on the new keyword.

BTW, I'm using VS 2010.

2 Answers 2

3

You need to initialize the _cruiseManager array before setting its elements inside the loop:

_cruiseManager = new CruiseServerHttpClient[2];
for (var i = 0; i < 2; i++)
{
    _cruiseManager[i] = new CruiseServerHttpClient();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Better to use List<CruiseServerHttpClient> instead.
1

List _cruiseManager=new List ();

_cruiseManager.Add(new CruiseServerHttpClient());

_cruiseManager.Add(new CruiseServerHttpClient());

this will do.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.