locked
[MVC] NullReferenceException when creating an entity ? RRS feed

  • Question

  • User-1370514677 posted

    Hi everyone,

    I don't understand this issue :

    NullReferenceException: Object reference not set to an instance of an object.
    
    => var new_article = new Article{

    Here is my code :

    Article.cs

    using System.ComponentModel.DataAnnotations;
    
    namespace WebApp.Models.Article
    {
        public class Article
        {
            [Key]
            public int Id { get; set; }
    
            public string AuthorId { get; set; }
            public string Title { get; set; }
            public int CategoryId { get; set; }
    
            public string Content { get; set; }
        }
    }

    PublishViewModel.cs

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using WebApp.Models.Article;
    
    namespace WebApp.ViewModels.Article
    {
        public class PublishViewModel
        {        
            [Required(ErrorMessage="Veuillez spécifier un titre")]
            [Display(Name="Titre : ")]
            public string Title { get; set; }
            
            public List<Category> CategoryList { get; set; }
    
            [Required(ErrorMessage="Veuillez sélectionner une catégorie")]
            [Display(Name="Catégorie : ")]
            public string CategoryName { get; set; }
    
            [Required(ErrorMessage="Veuillez saisir du texte")]
            [Display(Name="Article : ", Prompt="Saisissez votre article")]
            public string Content { get; set; }
        }
    }

    ArticleController.cs


    [HttpGet] public IActionResult Publish() { var publishViewModel = new PublishViewModel{ CategoryList = _articleDbContext.Categories.ToList() }; return View(publishViewModel); } [HttpPost] [ValidateAntiForgeryToken] public async Task<IActionResult> Publish(PublishViewModel publishViewModel) { if(!ModelState.IsValid) return View(publishViewModel); var article_category = _articleDbContext.Categories .Where(c => c.Name == publishViewModel.CategoryName) .SingleOrDefault(); var new_article = new Article{ AuthorId = (await _userManager.GetUserAsync(User)).Id, Title = publishViewModel.Title, CategoryId = article_category.Id, Content = publishViewModel.Content }; await _articleDbContext.Articles.AddAsync(new_article); await _articleDbContext.SaveChangesAsync(); return RedirectToAction("Index","Home"); }

    I've tried to debug but nothing, I don't understand why I'd get a NullReferenceException when creating a new Article entity.

    Best Regards

    Tuesday, March 9, 2021 3:25 PM

Answers

  • User-1370514677 posted

    Ok, I've just solved the issue by using the debbuger I found that one of my property was null : the current User.

    Indeed I forgot to apply [Authorize] flag on my controller which allowed non signed in user to use the method which is normally waiting for an Identity User.

    Thanks again

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 9, 2021 4:29 PM

All replies