none
response returning NullReferenceException RRS feed

  • Question

  • Hello,

    I'm having an issue returning data from a web API call.

    The web API function looks like this:

            public async Task<HttpResponseMessage> GetProjects()
            {
                var projects = _projectService.GetProjects().ToList();
                return Request.CreateResponse(HttpStatusCode.OK, projects, System.Net.Http.Formatting.JsonMediaTypeFormatter.DefaultMediaType);
            }

    The problem is that the results that return say there was a NullReferenceException. I know that the list of projects that's returned is not null or empty. I think it has something to do with the json formatter.

    Any suggestions?
    • Moved by Fei Hu Thursday, January 18, 2018 9:25 AM ASP.NET related
    Wednesday, January 17, 2018 6:49 PM

All replies

  • Hello,

     On what line of code does error occur? Also, is _projectService.GetProjects() the

    same method you defined as public async Task<HttpResponseMessage> GetProjects() ?

    The problem could be when Task execute its calling itself which does not fill projects

    with any data and the return does not execute.

     

     Hope this helps :)

    Wednesday, January 17, 2018 8:09 PM
  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, January 17, 2018 9:05 PM
  • Hello,

     On what line of code does error occur? Also, is _projectService.GetProjects() the

    same method you defined as public async Task<HttpResponseMessage> GetProjects() ?

    The problem could be when Task execute its calling itself which does not fill projects

    with any data and the return does not execute.

     

     Hope this helps :)

    I can't say what line of code the error occurs on because it occurs in transit from the web API to the main application. When I hover over the result object, I see this:

    The two GetProjects() are different. One belongs to a service, the other to the web api method which I posted.

    Wednesday, January 17, 2018 9:35 PM
  • it is where you should post.

    https://forums.asp.net/1246.aspx/1?Web+API

    Wednesday, January 17, 2018 10:07 PM
  • Hi Gibran,

    According to your question is more related to ASP.NET, you could repost the thread to ASP.Net forum for suitable support.

    If you have some grammar or code errors in using C#, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 18, 2018 9:24 AM