System.NullReferenceException : entity was null

0

I'm working under Visual Studio 2017.

I want to retrieve the comments the user is typing in the form. In order to do so, if there are no datas in the db, I'm using a post request, and if there are datas I'm using a put request. The post is working, but when there are datas, the put is returning the error System.NullReferenceException : entity was null.

Here is the code I'm working with :

Commentaire table controller :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MeteoDataAccess;

namespace MeteoCSSD___API.Controllers
{
    public class CommentaireController : ApiController
    {

        public IEnumerable<Commentaire> Get()
        {
            using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
            {
                return entities.Commentaire.ToList();
            }
        }

        public Commentaire Get(int id)
        {
            using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
            {
                return entities.Commentaire.FirstOrDefault(e => e.Id == id);
            }
        }

        public void Post([FromBody]Commentaire commentaire)
        {
            if (commentaire == null)
            {
                throw new ArgumentNullException(nameof(commentaire));
            }

            using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
            {
                entities.Commentaire.Add(commentaire);

                entities.SaveChanges();
            }
        }


        public void Put(int id, [FromBody]MeteoDataAccess.Commentaire commentaire)
        {
            if (commentaire == null)
            {
                throw new ArgumentNullException(nameof(commentaire));
            }

            using (MeteoCSSDEntities entities = new MeteoCSSDEntities())
            {
                var entity = entities.Commentaire.FirstOrDefault(e => e.Id == id);

                entity.Commentaire1 = commentaire.Commentaire1;

                entities.SaveChanges();
            }
        }

        public HttpResponseMessage Options()
        {
            var response = new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK
            };
            return response;
        }
    }
}

Frontend Function calling the request :

  CheckCommentaire(){
  if (this.commentaireList.length === 0 ) {
    if (this.comm1.Commentaire1 != null) {
      this.sqlP.postCommentaire(this.comm1);
    }
    if (this.comm2.Commentaire1 != null) {
      this.sqlP.postCommentaire(this.comm2);
    }
    if (this.comm3.Commentaire1 != null) {
      this.sqlP.postCommentaire(this.comm3);
    }
    if (this.comm4.Commentaire1 != null) {
      this.sqlP.postCommentaire(this.comm4);
    }
    if (this.comm5.Commentaire1 != null) {
      this.sqlP.postCommentaire(this.comm5);
    }
  } else {
  if (this.comm1.Commentaire1 != null) {
    this.sqlP.putCommentaire(this.comm1.IdService, this.comm1);
  }
  if (this.comm2.Commentaire1 != null) {
    this.sqlP.putCommentaire( this.comm2.IdService, this.comm2);
  }
  if (this.comm3.Commentaire1 != null) {
    this.sqlP.putCommentaire(this.comm3.IdService, this.comm3);
  }
  if (this.comm4.Commentaire1 != null) {
    this.sqlP.putCommentaire( this.comm4.IdService, this.comm4);
  }
  if (this.comm5.Commentaire1 != null) {
    this.sqlP.putCommentaire( this.comm5.IdService, this.comm5);
  }
  }
  }

Here is the error Visual Studio is returning :

System.NullReferenceException
  HResult=0x80004003
  Message=La référence d'objet n'est pas définie à une instance d'un objet.
  Source=MeteoCSSD - API
  StackTrace:
   at MeteoCSSD___API.Controllers.CommentaireController.Put(Int32 id, Commentaire commentaire) in J:\GITLil\MeteoCSSD-API\MeteoCSSD - API\Controllers\CommentaireController.cs:line 57
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__0(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
asp.net
nullreferenceexception
asked on Stack Overflow Nov 18, 2019 by Holoule • edited Nov 19, 2019 by Dharman

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0