-1

I am using a 3rd party API and new to programming so I need some guidance.

There is a class InStreamInfo which has properties

Comments
Dimention
Names
InStreamAdditionalInfo[]

InStreamAdditionalInfo[] class has properties defined as

ID
AddInfoDescription

I am creating a class object of InStreamInfo as

   InStreamInfo _info=new InStreamInfo ();
   _info.Comments="Test Comment";
   _info.Dimention= "200x300";
   _info.Names="Test Names 1";

While InStreamAdditionalInfo[] is defined as

InStreamAdditionalInfo[] _infoAdd= new InStreamAdditionalInfo[1];

_infoAdd[0].ID=12345;

But here I am getting an error

Object reference not set to an instance of an object.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3754674
  • 105
  • 2
  • 9
  • 2
    Null references are always the same thing. You are trying to access the property of an object whose value is null. Check that link and it will show you every possible way that you can get that exception and how to repair it. – crthompson Sep 15 '14 at 02:47
  • 1
    P.S. If you instantiate an array of objects, it doesnt mean that the array is populated. – crthompson Sep 15 '14 at 02:48
  • i saw the link but it has so many things i am confused can you tell me what i am doing wrong in my solution?? – user3754674 Sep 15 '14 at 02:49
  • When you used the debugger what did you find? – HABO Sep 15 '14 at 03:20

1 Answers1

1

You're creating an array with one position, but the array in this single position is null. You need to initialize _infoAdd[0]. Something like this

_infoAdd[0] = new InStreamAdditionalInfo();
_infoAdd[0].ID=12345;

Is not very common on C# to define the size of the collection. Unless you have a specific reason for this you could just use a collection that automatically increase its size when needed. This is an example

var _infoAdd= new List<InStreamAdditionalInfo>();
_infoAdd.Add(new InStreamAdditionalInfo { ID = 12345 });
Claudio Redi
  • 67,454
  • 15
  • 130
  • 155
  • 2
    Deja vu. It seems in consecutive days we both find ourselves here at [so] looking at yet another ObjectRefNotSetToAnInstance exception. btw I didn't downvote you but in future its probably best we close these questions down with a link to the duplicate (that has all the info for people to diagnose this very common exception). – Jeremy Thompson Sep 15 '14 at 02:54
  • @Jeremy Thompson: I think his confusion is about how a collection is initialized, don't think a general reference about this error helps. He believes that initializing the collection automatically initialize each posistion with a new instance. – Claudio Redi Sep 15 '14 at 02:57