Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm amateur with mongodb and C# driver.
I try to Insert a document with array of sub-documents by class.
my codes are these:
This is my classes:
C#
public class Entity
        {

            public string name { get; set; }
            public string family { get; set; }
            public List<sc> score { get; set; }
        }

        public class sc
        {
            public int Math{ get; set; }

            public int literature{ get; set; }
        }


And this is my code for fill fields and insert document:
C#
var en = new Entity();
            var sc1 = new sc();
            var sc2 = new sc();
            sc1.Math= 14;
            sc1.literature= 15;
            sc2.Math= 16;
            sc2.literature= 19;
            en.score[0] = sc1;
            en.score[1] = sc2;
            en.name = "Esi";
            en.family = "Moja";
            collection.Insert(en);


But i get this error when i run it:
C#
Object reference not set to an instance of an object.


How can i fix this problem? Please help me.
Posted
Comments
ZurdoDev 19-Mar-14 21:44pm    
On which line of code? All that error means is that you haven't instantiated something.
Member 10683893 20-Mar-14 17:59pm    
Hi Ryan
On this line:

en.score[0] = sc1;

ZurdoDev 20-Mar-14 18:41pm    
en.score[0] is null.
Member 10683893 21-Mar-14 8:16am    
What must i do??
ZurdoDev 21-Mar-14 8:19am    
en.score[0] = new score()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900