5

I have a class like this,

public class person
{
 name Name {set; get;}
 string Address {set; get;}
}

public class name
{
  string First {set; get;}
  string Last {set; get;}
}

Now when I create the object an try to set the first or last name I get an error. "Object reference not set to an instance of an object."

person Person = new person();
Person.Name.First = "John";
Person.Name.Last ="Smith";

What am I doing wrong?

1
  • 5
    Not a solution to your problem, but you should follow the guidelines and use Pascal casing for type names. i.e. use Person rather than person. Commented Apr 5, 2010 at 16:54

7 Answers 7

10

You need to initialise the name class instance too

person Person = new person();
Person.Name = new name();
Person.Name.First = "A";

You could also do this in the constructor of person if you wish

public class person
    {
        public name Name { set; get; }
        string Address { set; get; }

        public person()
        {
            Name = new name();
        }
    }

Or even try

person Person = new person { Name = new name { First = "A", Last = "B" } };
Sign up to request clarification or add additional context in comments.

Comments

6

That is because the Name property is null. You haven't assigned it a value.

Try this:

person p = new person();
p.Name = new Name();
p.Name.First = "John";
p.Name.Last = "Smith";

Or using object initializer syntax available from C# 3.0 and newer:

var p = new person { Name = new name { 
    First = "John", Last = "Smith" } 
};

1 Comment

According to their type names, this should be person p = new person();. Not that I agree with their naming convention.
2

Use the object initializer. Although it's very verbose for complex objects in your example you can do

new Person { Name = { First = "John", Last = "Smith" } };

Comments

1

You have to initialize the name property of person. Try:

person Person = new person(); 
person.name = new name();
Person.Name.First = "John"; 
Person.Name.Last ="Smith";  

WHen you define a class or struct, the C# language (and the CLR really) assigns defaults to all fields of the object when it is instantiated. The default are specific to the type of the field, but for references (which Name is in your case) the default is null. You are responsible for instantiating objects.

An alternative way to handle this situation would be to create a default name instance in the constructor of the person class; like so:

public class person  
{  
  name Name {set; get;}  
  string Address {set; get;}  

  public Person() { Name = new name(); }
}  

Comments

1

Given the code you want to use, just initialize your name object in person's constructor.

On a deeper level, what you're doing wrong is violating the Law of Demeter by accessing Name's properties outside of the Person class.

Comments

0

Your class needs a constructor which will assign values to your properties when new is used to create an instance of your class.

Comments

0

Try to initialize person and name

person Person = new person(); 
person.name = new name();
Person.Name.First = "John"; 
Person.Name.Last ="Smith"; 

Check this article for an overview on Naming convention.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.