-2

System.NullReferenceException occurred HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source= StackTrace: at mongotest.Program.Main(String[] args) in C:\Users\kiit\source\repos\C#-code\mongotest\mongotest\Program.cs:line 20

Please help me to solve this error. Why I get the null reference. Apart from this tell me how to get data in MongoDB for this schema

{ 

        "_id" : ObjectId("5a78a7365a98c0a0d9118c1a"), 
        "name" : "Amit", 
        "contacts" : [
            "userid1", 
            "userid2"
        ], 
        "logs" : {
            "status" : "online", 
            "Available" : "False"
        }
    }

Code : how to set value for logs and contacts,I want to insert these value in database. What I will do with this solution: I will insert value in a database on click event in asp.net and take values from the user.

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using MongoDB.Bson;
    using MongoDB.Driver;
    namespace mongotest
    {
        class Program
        {
            static void Main(string[] args)
            {
                /* Connection in MongoDb and insert data in database */
                var client = new MongoClient("mongodb://localhost");
                var database = client.GetDatabase("SIH");
                var collection = database.GetCollection<testing>("testing");
                testing newobject = new testing();
                newobject.name = "Amit Mishra";
                newobject.logs.Available = "true";
                newobject.logs.status = "false";
                collection.InsertOneAsync(newobject);
                Console.Write("Done");
                Console.Read();
            }
        }
    }

    public class testing
    {
        public string _id { get; set; }
        public string name { get; set; }
        public string[] contacts { get; set; }
        public Logs logs { get; set; }
    }

    public class Logs
    {
        public string status { get; set; }
        public string Available { get; set; }
    }
  • It is a bit of a beginner programmer mistake: the error message says exactly what the problem is: code has not been initialized on line 20. So simply check what it is you are trying to do there and notice that the 'logs' property has not been initialized. – Michaël van der Haven Feb 05 '18 at 20:04
  • Thanks Could you please help me to know how to "contacts" : [ "userid1", "userid2" ], set these value (userid1 and userid2) – AMIT MISHRA Feb 05 '18 at 20:07

2 Answers2

1

You're accessing a non-initialized property which is why you're getting the NullPointerException.

Change your code to this instead:

[...]
newobject.logs = new Logs();
newobject.logs.Available = "true";
[...]
dnickless
  • 9,409
  • 1
  • 11
  • 29
0

As indicated by dnickless, you have to initialize the logs, and you have to initialize the string array as well. In your case the code should be enhanced to something like:

[...]
newobject.logs = new Logs();
newobject.logs.Available = "true";
newobject.contacts = new string[]{ "userid1", "userid2" };
[...]