Why are you saying that this object is null?

2
papelX.moderador = abc.Id;
papelX.participantes.Add(abc);

I'm finding a NullReferenceException in this code above. I am creating a papelX object and will use some properties of the object abc .

Visual Studio is saying that on the bottom line the abc object is null. I'm not understanding, because in this first line, as you can see, the Id property of abc was properly copied to papelX.moderador . There was no problem.

Even with the mouse over abc I realize that several properties of it are properly filled. It is not null. Why is this happening?

    
asked by anonymous 23.06.2016 / 05:12

1 answer

4

The error is precisely because the participantes member of the papelX object is null. You can not try to add anything to nothing . You must first initialize this member, probably with a new List<Usuario>() , so you have an empty list there, you can add it here.

Under normal conditions this should already be done in property , field or #

It does not really seem like abc is null. And a null could be added to this List without generating error. If the intention is not to accept nulls, the code should consider this, possibly in the implementation in the participantes property. If it's a property (it should probably be and should follow the C # naming pattern using upper case).

I would help more if the question had more information, such as code, for example.

    
23.06.2016 / 05:41