Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Basics Modeling and Presenting Data Using Strongly Typed Views

Huy Tran
Huy Tran
344 Points

Object reference not set to an instance of an Object

HomeController.cs

namespace net.Controllers
{
    public class HomeController : Controller
    {


        public ActionResult Detail()
        {
        var home = new ComicBooks()
        {
            Artists = new Artist[]
            {
                new Artist() { Name = "Dan Slott", Role="Script" },
                new Artist() { Name = "Humberto Ramos", Role="Pencils"},
                new Artist() { Name = "Victor Olazaba", Role="Inks"},
                new Artist() { Name = "Edgar Delgado", Role="Colors"},
                new Artist() { Name = "Chris Eliopoulos", Role="Letters"},
            }
        };
        Console.WriteLine(home.Artists.Length);    
            return View(home);
        }
    }
}

ComicBooks.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;



namespace net.Models
{
    public class ComicBooks
    {
        public int Id{get;set;}
        public string SeriesTitle{get;set;}
        public int IssueNumber{get;set;}
        public string DescriptionHTMl{get;set;}
        public Artist[] Artists {get; set;} 
        public bool Favorite { get; set;}

        public string DisplayText 
        {
            get
            {
                return SeriesTitle + " #" + IssueNumber;
            }
        }
        public string CoverImageFiles
        {
            get
            {
                return SeriesTitle.Replace(" ","-").ToLower() + "-" + IssueNumber + ".jpg";
            }

        }


    }
}

Detail.cshtml

@page

@model net.Models.ComicBooks

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
    var description = "<p>Final issue! Witness the final hours of Doctor Octopus' life and his one, last, great act of revenge! Even if Spider-Man survives... <strong>will Peter Parker?</strong></p>";
}

        <div>
            @Html.Raw(description)
        </div>
        <div>
            <ul>
                @foreach(var artist in Model.Artists)
                {
                    <li>
                        @artist.Name
                    </li>
                }
            </ul>
        </div>

I am using asp.net core 2.0. Keep getting this error for some reason, Please help

Steven Parker
Steven Parker
231,248 Points

The entire error message should identify a file, line and column. What's the rest of it?

Huy Tran
Huy Tran
344 Points
Microsoft.AspNetCore.Server.Kestrel[13]
      Connection id "0HLC95AVGB5OA", Request id "0HLC95AVGB5OA:00000002": An unhandled exception was thrown by the application.
System.NullReferenceException: Object reference not set to an instance of an object.
   at AspNetCore._Views_Home_Detail_cshtml.get_Model()
   at AspNetCore._Views_Home_Detail_cshtml.<ExecuteAsync>d__0.MoveNext() in /Users/MaiKhoi/Documents/net/Views/Home/Detail.cshtml:line 15

So the error come from this line

 @foreach(var artist in Model.Artists)

1 Answer

Amar Resic
Amar Resic
2,922 Points

I'm having the same problem but in the if statement @if (Model.Artists.Length > 0).