Object reference error getting when I'm trying to put data using WebAPI2. My class structure is like this
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Address address { get; set; }
}
public class Address
{
public int Id { get; set; }
public int StudentId { get; set; }
public string City { get; set; }
public string AddressLine { get; set; }
}
My Put method is like below
public async Task putStudentProfile(Student student)
{
tblStudent std= db.tblStudent .FirstOrDefault(p => p.ID == student.Id);
std.NAME = student.Name;
std.EMAIL= student.Email;
tblAddress addr= db.tblAddress.FirstOrDefault(q => q.ID == student.Id);
addr.City= student.address.City;
await db.SaveChangesAsync();
}
Sending JSON data using postmen. My JSON data is like this
{
"Id":59,
"Name": "Sandeep",
"Email": "xxxxxxxx@xxxx.com",
"address":{
"Id":42,
"StudentId": "59",
"City": "xxxxxx",
"AddressLine":"xxxxxxxxxxxxx"
}
}
I'm getting object reference error when I try to access student.address.City
Any idea to solve this?