0

I define a class comprising of a list of strings and try to initialise the object but I get an error "object reference not set to an instance of an object"

Here is the code defining the class

public class TopClass
    {
        public List<string> Stringlist { get; set; }

    }

then I try to add data to the list and get the "object reference not set to an instance of an object".

 TopClass topClass = new TopClass();

        topClass.Stringlist.Add("house");

What could I be doing wrong?

Jeets
  • 1
  • 1
  • 5
    You need to initialize it first `topClass.Stringlist = new List();` or you can initialize it during creation `public List Stringlist {get; set;} = new List();` and really you probably don't need it to have a `set`. – juharr May 29 '20 at 00:05

0 Answers0