System.NullReferenceException: Object reference not set to an instance of an object.
See original GitHub issueHi,
just started using JADNC (v2.3.4) with my .NET Core project (v.2.1.0). So far I got my API working with JSON API that I am able to POST and GET my data properly.
However, when I attempt to GET an item by Id it shows/throws the following error:
System.NullReferenceException: Object reference not set to an instance of an object.
at JsonApiDotNetCore.Builders.DocumentBuilder.Build(IIdentifiable entity)
in C:\projects\jsonapidotnetcore\src\JsonApiDotNetCore\Builders\DocumentBuilder.cs:line 37
at JsonApiDotNetCore.Serialization.JsonApiSerializer.SerializeDocument(Object entity)
in C:\projects\jsonapidotnetcore\src\JsonApiDotNetCore\Serialization\JsonApiSerializer.cs:line 87
at JsonApiDotNetCore.Formatters.JsonApiWriter.WriteAsync(OutputFormatterWriteContext context)
in C:\projects\jsonapidotnetcore\src\JsonApiDotNetCore\Formatters\JsonApiWriter.cs:line 36
My model that I’m trying to access looks like this: (Holiday.cs)
[Table("Feiertag")]
public class Holiday : Identifiable
{
public Holiday()
{
}
[Column("Name")]
[MaxLength(64)]
[Attr("name")]
public string Name { get; set; }
[Column("Datum")]
[Attr("date")]
public DateTime Date { get; set; }
}
The corresponding controller like this: (HolidaysController.cs)
/// <summary>
///
/// </summary>
[ApiController]
[ValidateModel]
[Route("api/[controller]")]
[Produces("application/vnd.api+json")]
[EnableCors(AntaresNames.DefaultCorsPolicy)]
public class HolidaysController : JsonApiController<Holiday>
{
/// <summary>
///
/// </summary>
/// <param name="jsonApiContext"></param>
/// <param name="resourceService"></param>
public HolidaysController(
IJsonApiContext jsonApiContext,
IResourceService<Holiday> resourceService)
: base(jsonApiContext, resourceService)
{
}
}
I’m using Postman to send a GET request to my backend using the following URL:
https://localhost:44350/api/holidays/2
What am I missing here? Like I stated above, the “normal” GET works perfectly fine…
EDIT: Please tell me if I heend to provide additional information/code to the issue!
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Object Reference Not Set to an Instance of an Object
This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a...
Read more >c# - What is a NullReferenceException, and how do I fix it?
The message "Object not set to an instance of Object" means you are trying to use an object which has not been initialized....
Read more >How can I fix the error: System.NullReferenceException
When you have a statement like A.B.C = E.F; , and you receive an NullReferenceException 'Object reference not set to an instance of...
Read more >Object reference not set to an instance of an object” error?
The “Object reference not set to an instance of an object” is a very famous error in C# that appears when you get...
Read more >Get to the Bottom of C# Object Reference Not Set to an ...
So, this error description says that an object that is being called to get or set its value has no reference. This means...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey @ewolution94 ,
Did you configure the JADNC middleware in your Startup.cs and apply your DbContext (if applicable)
https://json-api-dotnet.github.io/#/step-by-step
If you post your Startup.cs that would be helpful as well.
@jaredcnance there seems to be enough details to decide what to do with this further. I was able to reproduce by following the steps above - basically posting data that doesn’t conform to validation attributes while using Content-Type: application/vnd.api+json.
I’m new to this codebase but it looks to me that the DocumentBuilder fails to notice that there were validation errors and tries to serialize ValidationProblemDetails object as if it was IIdentifiable resource.
This issue makes debugging quite difficult because an error in the filter bypasses a lot of logging so there is no easy way to find out what data didn’t get past validation… Suppressing the filter helps but is not a great solution.