-2
public class StudentNames : INotifyPropertyChanged
{
   string firstname
   string surname

   public string FirstName
    {
        get
        {
            return firstname;
        }
        set
        {
            if (firstname != value)
            {
                firstname = value;
                OnPropertyChanged("FirstName");
            }
       }
    }

    public string Surname
    {
        get
        {
            return surname
        }
        set
        {
            if (surname != value)
            {
                surname = value;
                OnPropertyChanged("Surname");
            }
       }
    }
 }

public class StudentDetails : INotifyPropertyChanged
{
    string address StudentNames sn

    public string SN
    {
        get
        {
            return sn;
        }
        set
        {
            if (sn != value)
            {
                sn = value;
                OnPropertyChanged("SN");
            }
       }
    }

    public string Address
    {
        get
        {
            return address;
        }
        set
        {
            if (address != value)
            {
                address = value;
                OnPropertyChanged("Address");
            }
      }
    }
 }

elsewhere I have the logic:

StudentDetails sd = new StudentDetails()
sd.StudentNames.FirstName = "John"//this part gives me a runtime error

It compiles but I get a runtime error :

"Object reference not set to an instance of an object."

nonsensickle
  • 4,438
  • 2
  • 34
  • 61
billiard
  • 1
  • 2
  • `sn` is a `string` or an instance of `StudentNames`? It is not clear. Please format and correct your code. – Kryptos Jun 15 '15 at 22:24

1 Answers1

1

Your public constructor should look like this:

public StudentDetails() {
  StudentNames = new StudentNames();
}

You got error, because you haven't set object reference to StudentNames field. Then you can do

var sd = new StudentDetails():
sd.StudentNames.FirstName = "John"

Also, next time you should better add full code of class.

ivamax9
  • 2,601
  • 24
  • 33
  • ... Next time the OP should do a search first, which yields the well known question: http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it. Then the OP should follow one suggestion that states: use a Debugger ... sorry for being a bit cynical. –  Jun 15 '15 at 22:28