0

I have been learning C# and ASP.NET Core for around 1 month. I have a problem with this NullRefrences error and I can't find out the reason. I create a news webpage by asp.net core and in the administration section when I create a comment section for the article I get the "NullReference" error to show comments belong to which article. please help me out on this one. Article Model:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace WebNews.Models
{
    public class Article
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Body { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime PublishedDate { get; set; }
        public string Writer { get; set; }
        public string Photo { get; set; }
        public bool IsPublished { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime LastModified { get; set; }
        public string Categories { get; set; }
        public virtual ICollection<Comment> Comments { get; set; }

   }
}

this is my comment Model:

using System;
using System.ComponentModel.DataAnnotations;

namespace WebNews.Models
{
    public class Comment
    {

    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Body { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime CommentDate { get; set; }
    public virtual Article Article { get; set; }
    public bool IsProved { get; set; }
    }
}

This is my Controller:

    [HttpGet]
    public IActionResult ListComments()
    {
        var comments = _comment.AllComments();
        AdministrationListCommentViewModel model = new AdministrationListCommentViewModel();
        foreach (var comment in comments)
        {
            AdministrationListCommentsViewModel modell = new AdministrationListCommentsViewModel
            {
                ArticleTitle = article.GetArticle(comment.Article.Id).Title,
                Body = comment.Body,
                Id = comment.Id,
                IsProved = comment.IsProved,
                PublishedDate = comment.CommentDate,
                Title = comment.Name,
            };

            model.Comments.Add(modell);
        }
        return View(model);
    }

this my view models: both of them are here:

 using System;
 using System.ComponentModel.DataAnnotations;
 using WebNews.Models;

 namespace WebNews.ViewModels
 {
     public class AdministrationListCommentsViewModel
     {
    public int Id { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
    public Article Article { get; set; }
    public bool IsProved { get; set; }
    [DataType(DataType.DateTime)]
    public DateTime PublishedDate { get; set; }
    public string ArticleTitle { get; set; }

    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using WebNews.Models;

namespace WebNews.ViewModels
{
    public class AdministrationListCommentViewModel
    {
        public List<AdministrationListCommentsViewModel> Comments { get; set; }
    }  
}

and this is my view:

    <div class="card">
    <div class="card-header">
        <h5 style="display: inline-block">Article`s List</h5>
        <button style="display: inline-block; position:fixed; left: 1600px; top: 18px; z-index: 9999; background-color:#22119d; " type="submit" class="btn btn-primary m-md-n1">Update</button>
    </div>
    <table class="table" style="width: 1570px;">
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Author</th>
                <th scope="col">Edit</th>
                <th scope="col">Article</th>
                <th scope="col">Is Published</th>
            </tr>
        </thead>
        @for (int i = 0; i < Model.Comments.Count; i++)
        {
            <tbody>
                <tr>
                    <th scope="row">@number </th>
                    <td>@Model.Comments[i].Title</td>
                    <td>
                        <a href="#">Edit</a>
                    </td>
                    <th scope="row">@Model.Comments[i].ArticleTitle</th>
                    <td>
                        <div class="form-group m-1">
                            <input class="form-check-input" style="position: relative; left: 50px; top: -3px;" type="checkbox" value="@Model.Comments[i].IsProved">
                            <label class="form-check-input" asp-for="@Model.Comments[i].IsProved" hidden></label>
                        </div>
                    </td>
                </tr>
            </tbody>
            @(number = number + 1);

        }

    </table>
    </div>

I don't even know is this how you make comment for the article or not or is there any better way. I have used view model in the controller because I wanted to be able to edit isproved of my comment on the comment list page. when I run these codes I get the "NullReference" error for that article title.

Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Roman Marusyk Dec 18 '19 at 16:54
  • What does `_comment.AllComments()` look like? I suspect `comment.Article` is `null` in the `foreach` because the navigation property is likely not `.Include`d – Matt U Dec 19 '19 at 00:56
  • in _comment.AllComment i take all of the comment and store it in variable which here is comments then i use foreach loop to pass the comments to the view model but the only problem there article which is parent table doesnt get added to view model and i get null but other properties are just fine. – Sahand salmani Dec 19 '19 at 04:08

0 Answers0