GABRIEL22
GABRIEL22
CC#
Created by GABRIEL22 on 1/15/2024 in #help
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 .
16 replies
CC#
Created by GABRIEL22 on 1/11/2024 in #help
How succesfully set a Database Relationship in .net?
No description
13 replies
CC#
Created by GABRIEL22 on 1/10/2024 in #help
How to fix " The ConnectionString property has not been initialized." in .Net api
No description
7 replies
CC#
Created by GABRIEL22 on 1/4/2024 in #help
How to add data into one-to-one relationship
No description
50 replies
CC#
Created by GABRIEL22 on 12/30/2023 in #help
How to fix "Assert.IsAssignableFrom() Failure: Value is null" in Xunit testing?
Hello world! I'm writting some test for a TDD app in Asp. However when I test a Create(Post) controller, the server return me: Assert.IsAssignableFrom() Failure: Value is null. Here's the repository: And here's the controller: Finally, this is the xUnit test code: I must mention that I'm not using Moq library nor Memory database, just simple Lists. I'm not sure if this detail may help. Anyway, I'd be really grateful if anybody could bring me some help because I'm new on this.
1 replies