-3
class Program

{
    class Chair
    {
        public Array[] people { get; set; }
    }

    static void Main(string[] args)
    {
       var chr = new Chair();
       if(chr.people.Length > 0) Console.WriteLine("got eeem");
}

I have tried to do it like this also:

class Program

{

    Chair chr { get; set; }

    class Chair
    {
        public Array[] people { get; set; }
    }

    static void Main(string[] args)
    {
       var pr = new Program();
       if(pr.chr.people.Length > 0) Console.WriteLine("got eeem");
}

I keep getting the same error:

Object reference not set to an instance of an object.

When checking if the length is greater than 0 (Eventually I'll be adding to it).

Why can't I check its greater than 0? I instance it in two ways and neither work?

Thanks in advance!

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Mage Xy Mar 24 '16 at 14:41
  • Where exactly do you instantiate it? – Transcendental Mar 24 '16 at 14:41
  • You need to instantiate the people array. And do that inside your constructor. for Chair Also, make the people array private and access it via a property or method instead. – ManoDestra Mar 24 '16 at 14:47

4 Answers4

2

You have to new up your chr.people array also. Preferably in the Chairs constructor

andreasnico
  • 1,478
  • 14
  • 23
  • While your answer is pretty much correct, it would be better served by taking the asker's provided code and adding the item you're talking about. – MattD Mar 24 '16 at 14:45
1

your people array is null

Instantiate it in a constructor for chair

class Chair
{
    public Chair()
    {
        people = new Array[0];
    }

    public Array[] people { get; set; }
}

or check for null before checking the length.

if(chr.people != null && chr.people.Length > 0) Console.WriteLine("got eeem");

or if your using the latest:

if (chr.people?.Length > 0) Console.WriteLine("got eeem");
moi_meme
  • 9,180
  • 4
  • 44
  • 63
1

chr.people is null (undefined). You would need to assign it, or provide a default (empty) array in the Chair's ctor.

class Chair
{
    public Array[] people { get; set; }

    public Chair()
    {
        people = new Array[0]; // instantiate here
    }
}

Now when you create a new Chair you'll have an empty array (that you can access Length property of).

Alternatively you could assign it when you instantiate chr:

Chair chr = new Chair();
chr.people = new Array[0];
// now you can access chr.people.Length

The key take-away here is the default value (when not explicitly defined) for an array (in this case Array[]) is null, so there's no .Length to access. You can either anticipate this (with a check for .people != null first), or initialize it when you create the object.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • Oh, that makes so much more sense! Appreciate this so much! will mark answer when possible – Jaquarh Mar 24 '16 at 14:41
  • solved my issue, also changed it to a `List` for ease and had to instance that also :) appreciated! its all working now :D – Jaquarh Mar 24 '16 at 14:49
1

The problem is that the "people" property is not being initialized. You have several approaches.

if (chr.people !=null && chr.people.Length > 0) Console.WriteLine("got eeem");

Or make sure that people is initialized

    public class Chair
    {
        public Chair()
        {
            people = new Array[0];
        }

        public Array[] people { get; set; }
    }

The first is typical defensive coding. eg What happens if someone does chr.people=null;

The problem is that you are not encapsulating the concerns of Chair within the class. Which means that everything that uses a Chair must be aware of it's limitations. The Chair class must be designed such that it provides a safe interface and protects it's resources (ie the people property).

Perhaps something like this:

    public class Chair
    {
        private List<Person> people = new List<Person>();
        public IEnumerable<Person> People { get { return people; } }

        public void AddPerson(Person person)
        {
            people.Add(person);
        }
        public void RemovePerson(Person person)
        {
            people.Remove(person);
        }
        public void ClearPeople(Person person)
        {
            people.Clear();
        }
    }   
    public class Person
    {
        public string Name { get; set; }
    }

IEnumerable only allows things like "foreach" so no one can mess with the contents of the collection unless they use the Add/Remove/Clear methods.

You could just make People a List< Person> but you have to be ok with exposing everything to the consumer of your class.

AndyPook
  • 2,762
  • 20
  • 23