John
John

Reputation: 1922

How to initialize an Object?

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?

Upvotes: 5

Views: 10329

Answers (7)

systempuntoout
systempuntoout

Reputation: 74134

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.

Upvotes: 0

Austin Salonen
Austin Salonen

Reputation: 50235

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.

Upvotes: 1

JYelton
JYelton

Reputation: 36536

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

Upvotes: 0

LBushkin
LBushkin

Reputation: 131806

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(); }
}  

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166506

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" } };

Upvotes: 9

Chris Marisic
Chris Marisic

Reputation: 33118

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" } };

Upvotes: 2

driis
driis

Reputation: 164341

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" } 
};

Upvotes: 6

Related Questions