C
C#11mo ago
GABRIEL22

System.NullReferenceException: Object reference not set to an instance of an object.

I'm developing a Asp.net Api which consist in the updating a existing Object into another, but when I use the controller, return the following error: System.NullReferenceException: Object reference not set to an instance of an object. This is the model:
public class Teacher {

[Key]
public int? Dni {get; set; }
public string? FirstName {get; set; }
public string? SurName {get; set; }
public string? Email {get; set; }
public string? Password {get; set;}
public bool? Status {get; set; }

}

public class Subject
{
public string? id {get;set;}
public string? Title {get; set; }
public int? Credits {get; set; }

public int? TeacherDni {get; set;}
[ForeignKey(nameof(TeacherDni))]
public virtual Teacher Teacher {get; set;}

}
public class Teacher {

[Key]
public int? Dni {get; set; }
public string? FirstName {get; set; }
public string? SurName {get; set; }
public string? Email {get; set; }
public string? Password {get; set;}
public bool? Status {get; set; }

}

public class Subject
{
public string? id {get;set;}
public string? Title {get; set; }
public int? Credits {get; set; }

public int? TeacherDni {get; set;}
[ForeignKey(nameof(TeacherDni))]
public virtual Teacher Teacher {get; set;}

}
The controller logic consist in the searching through Subject to find whether a teacher is settled (I mean when it's not null) or not. If is does exist, then search for a existing Teacher and set teacher using his Dni as a foreign key :
[HttpPut("/Enroll/{subjectid}")]

public IActionResult EnrollTeacher(string subjectid, int teacherDni)
{

var subject = _context.Subject.GetSubjectById(subjectid);

if(subject.TeacherDni == null) //NullReferenceException
{
return NotFound();
}
var teacher = _context.Teacher.GetTeacherbyDni(teacherDni);

subject.TeacherDni = teacher.Dni;

_context.UnitOfWork.SaveChanges();

return Ok(subject);
}

[HttpPut("/Enroll/{subjectid}")]

public IActionResult EnrollTeacher(string subjectid, int teacherDni)
{

var subject = _context.Subject.GetSubjectById(subjectid);

if(subject.TeacherDni == null) //NullReferenceException
{
return NotFound();
}
var teacher = _context.Teacher.GetTeacherbyDni(teacherDni);

subject.TeacherDni = teacher.Dni;

_context.UnitOfWork.SaveChanges();

return Ok(subject);
}


I must to mention that I'm doing this with existing data of database's seed .
8 Replies
GABRIEL22
GABRIEL22OP11mo ago
The error occur when I tried to access the object to check if is null :
cs

if(subject.TeacherDni == null)
cs

if(subject.TeacherDni == null)
ThatDaniel
ThatDaniel11mo ago
GetSubjectById is returning null then, whats the code for GetSubjectById ?
Jimmacle
Jimmacle11mo ago
that means subject is null, you can't access a member of a null reference
GABRIEL22
GABRIEL22OP11mo ago
To get a subject by his id (primary)
Denis
Denis11mo ago
You could do this subject?.TeacherDni== null
GABRIEL22
GABRIEL22OP11mo ago
I mean the Subject exist in the database. I'm using data seeded of database migration.
Jimmacle
Jimmacle11mo ago
i don't care about that, i'm telling you what's wrong with your code if you are getting a NRE on that line, subject is null so you have to find out why that is
Angius
Angius11mo ago
The FK will be loaded, but not necessarily the navigation property You have a virtual keyword there, so are you using lazy loading? If so, then stop If not (or after following the previous step) load the related property with .Include() Or, better yet, use a .Select() and select only the data you need into a DTO class
Want results from more Discord servers?
Add your server