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

AG Mohamed 1 Reputation point
2021-06-15T10:08:21.097+00:00

I'm new to asp .net core i have a issue related to `

Object reference not set to an instance of an object

.I created class names are destination , siteDestination ,site
destintination table has

destination_id ,name fields

Site table has siteId, sitecode fields

siteDestination table has composite key destination_id and siteId are integer

siteDestination and destintination have many-many relationship

I need to update destination table siteDestinationField so i get input as "siteDestinations":[1,2] in postman

i created dto object that has name, public List<int> SiteDestinations{ get; set; } property

i set mapper to dto class and destination class and ignore SiteDestinations property

when request made "siteDestinations":[1,2] it come to dto object .using mapping i translate into destination object except SiteDestinations

i check particular id using get method

then

destination.SiteDestinations.Add(new SiteDestination()); (debug untill then exception occur

then i call
var length=destinationDTO.SiteDestinations.Count;

            for(var i=0;i<length;i++){

                destination.SiteDestinations[i].DestinationId=destination.Id;

                destination.SiteDestinations[i].SiteCodeId=destinationDTO.SiteDestinations[i];
            }

when debug AND SEND REQUEST FROM POSTMAN it ignore the loop when debug finished it indicate

Object reference not set to an instance of an object. AS ERROR

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-15T20:51:40.017+00:00

    You need to debug the WebAPI and find out what line it blows up on. You can use debug Quickwatch in then Visual Stuido IDE to look at the context of an object. If the object is null, then that is way the exception is being thrown becuase the object doesn't exist in memory.

    You can set the WebAPI as the startup project, configure the project to use the Web browser, you can configure the URL just like you did when using Postmon in the browser address line, and set a breakpoint in the code and start single stepping the code.

    https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.