1

I'm super super tired right now and probably have done a big rookie mistake. I'm doing a ViewModel to grab all my models in 1 main model class. So far I've only done News and already hitting in a error..

See error:

NullReferenceException: Object reference not set to an instance of an object. AspNetCore._Views_Home_Index_cshtml+d__21.MoveNext() in Index.cshtml + @foreach(News news in Model.Newss)

(Yes my naming is messed up but wanted to see foundation worked first).

Anyways: News.cs :

namespace UKSF.Models
{
    public class News
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Text { get; set; }
        public string Publisher { get; set; }
    }
}

MainViewModels.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

    namespace UKSF.Models
    {
        public class MainViewModel
        {
            public List<News> Newss { get; set; }
        }

    }

index.cshtml:

@model UKSF.Models.MainViewModel 
@{
    ViewData["Title"] = "Home Page";
}

    @foreach(News news in Model.Newss)
    {
        @news.Title
    }

EDIT: As you guys pointed out, the error was due of me not initializing it, I believed I had already done that in Controller page. Now its:

public IActionResult Index()
{
    MainViewModel model = new MainViewModel();
    model.Newss = new List<News>();
    return View(model);
}
Julian Alwandy
  • 181
  • 1
  • 11
  • Are you initializing the `Newss` property to a list before using it ? If not it will be `NULL`! – Shyju Mar 22 '17 at 15:49

3 Answers3

3

Are you ever initializing your Newss collection prior to using it? If not, it will be null and will throw a NullReferenceException when you attempt to iterate through it in your foreach loop.

Consider initializing it in a constructor:

public class MainViewModel
{
        public List<News> Newss { get; set; }

        public MainViewModel()
        {
             // This will initialize the collection when a new instance of
             // your model is created
             Newss = new List<News>();
        }
}

or inline using an auto-property initializer:

// This will set the default value to a new list instead of null
public List<News> Newss { get; set; } = new List<News>();
Graham
  • 6,577
  • 17
  • 55
  • 76
Rion Williams
  • 69,631
  • 35
  • 180
  • 307
  • Thank you +1 That was the correct solution, I thought I had done it. Now I just have to retrieve context and happy days! Thank you once again!! – Julian Alwandy Mar 22 '17 at 15:55
  • Just a question.. ApplicationDbContext, what is the best way to utilize it to retrieve i.e "title" rows from News model? – Julian Alwandy Mar 22 '17 at 16:10
0

Initialize your list

   using System;
   using System.Collections.Generic;
   using System.Linq;
  using System.Threading.Tasks;

namespace UKSF.Models
{
    public class MainViewModel
    {
        public MainViewModel()
        {
           Newss = new List<News>();
        }
        public List<News> Newss { get; set; }
    }

}
Jinish
  • 1,863
  • 1
  • 14
  • 21
0

You never initialized your list. Create a constructor and initialize it there.

Newss = new List<News>();

EDIT: Or you can do it in-line like Rion's answer.

Rafael
  • 797
  • 13
  • 29