Why are you saying this object is null?

Asked

Viewed 334 times

2

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

I’m finding a NullReferenceException in that code above. I’m creating an object papelX and I will use some properties of the object abc.

Visual Studio is saying that at the bottom line the object abc is void. I don’t understand, because on that first line, as you can see, the property Id of abc was duly copied to papelX.moderador. There was no problem.

Including putting your mouse on top of abc I realize that several of his properties are duly filled in. It is not null. Why this is happening?

  • 1

    So it must be magic. Or the problem is not quite this one. It has very little information for us to help. If you can put more relevant things in, then you can help. I did not see the exact error, if the line is this same, if there is anything else that might be interfering. It may be that the participantes is null and is misinterpreting the error. Even if the abc is null it is likely that this is no problem, it is possible to add nulls to data collections. I almost put this as an answer, since that’s just what gives p/ respond now.

  • Hi @bigown, thanks for trying to help. It’s my first question here so I don’t know how to write it. O participantes is null yes. It is an object List<Usuario>, then hopes to receive exactly the kind of abc. But it starts with nothing because I just created this object papelX some lines up and I’m populating the fields. Abc would be the first participant in the case. I’m doing something wrong?

  • Before papelX.participantes.Add(abc); try to put papelX.participantes = new List<Participantes>();. Note that in List<Participantes>(), Participants is the Model that will compose the list.

  • Did the answer solve your problem? Do you think you can accept it now? See [tour] to understand how it works. It would be helpful to indicate to everyone that the solution was useful and satisfactory for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

5

The mistake is precisely because the member participantes of the object papelX is null. You cannot try to add anything in nothingness. First you need to initialize that member, probably with a new List<Usuario>(), so you have an empty list there, then you can add.

Under normal conditions this should already be done in the property, field or class builder (preferably in that order), so the object is not in invalid state ever.

It really doesn’t seem that the abc is null. And a null could be added to that List without generating error. If the intention is not to accept nulls, the code should consider this, possibly in the implementation in the property participantes. If it is a property (probably should be and should follow the C nomenclature standard# capitalizing).

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

  • I tried to be succinct but I’ll explain a little better: I have two classes (entities) in my project: Papel and Usuario. Paper owns this property participantes which is nothing more than a list of users who participated ( List<Usuario> ). In the following code, I urge the Usuario abc, fill it with the information coming from Javascript, then Papel papelx and I’ll fill in the properties:

  • JavaScriptSerializer js = new JavaScriptSerializer();&#xA; Usuario abc = new Usuario(); &#xA; abc = js.Deserialize<Usuario>(UsuarioLogado);&#xA;&#xA; using (Contexto db = new Contexto())&#xA; {&#xA; Papel papelX = new Papel();&#xA; papelX.preenxido = false;&#xA; papelX.moderador = abc.Id;&#xA; papelX.participantes.Add(abc);&#xA; }&#xA; Should I also instantiate the User List? Where? Thanks @bigown

  • @Riquecardoso has to instantiate, as I already put in the answer. Where? I do not know, if you had put in the question your code I would know. You can still edit there and put. The problem seems to be in the class Papel, as I said she seems to be poorly structured.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.