question

AGMohamed-3618 avatar image
0 Votes"
AGMohamed-3618 asked DuaneArnold-0443 answered

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

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












dotnet-aspnet-core-webapi
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered

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://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.