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 trialAdam Shockley
2,543 PointsAdd Series List and Detail Views (Centralizing Our Data)
I am getting a NullReferenceException on run. I imagine something is wrong with either my InitData or my call to InitData. I've been banging my head on the keyboard a while now. Can anybody help me out.
This is my ComicBookRepository
using ComicBookGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ComicBookGallery.Data
{
public class ComicBookRepository
{
public ComicBook[] GetComicBooks()
{
return Data.ComicBooks;
}
public ComicBook GetComicBook(int id)
{
ComicBook comicBookToReturn = null;
foreach (var comicBook in Data.ComicBooks)
{
if (comicBook.Id == id)
{
comicBookToReturn = comicBook;
break;
}
}
return comicBookToReturn;
}
}
}
And this is my data
using ComicBookGallery.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace ComicBookGallery.Data
{
public static class Data
{
public static ComicBook[] ComicBooks { get; private set; }
public static Series[] Series { get; private set; }
static Data()
{
InitData();
}
private static void InitData()
{
var series = new Series[]
{
new Series()
{
Id = 1,
Title = "The Amazing Spider-Man",
DescriptionHtml = "<p>The Amazing Spider-Man (abbreviated as ASM) is an American comic book series published by Marvel Comics, featuring the adventures of the fictional superhero Spider-Man. Being the mainstream continuity of the franchise, it began publication in 1963 as a monthly periodical and was published continuously, with a brief interruption in 1995, until its relaunch with a new numbering order in 1999. In 2003 the series reverted to the numbering order of the first volume. The title has occasionally been published biweekly, and was published three times a month from 2008 to 2010. A film named after the comic was released July 3, 2012.</p>"
},
new Series()
{
Id = 2,
Title = "Bone",
DescriptionHtml = "<p>Bone is an independently published comic book series, written and illustrated by Jeff Smith, originally serialized in 55 irregularly released issues from 1991 to 2004.</p>"
}
};
var comicBooks = new ComicBook[]
{
new ComicBook()
{
Id = 1,
Series = Series[0],
IssueNumber = 700,
DescriptionHtml = "<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>",
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" },
},
Favorite = false
},
new ComicBook()
{
Id = 2,
Series = Series[0],
IssueNumber = 657,
DescriptionHtml = "<p><strong>FF: THREE TIE-IN.</strong> Spider-Man visits the FF for a very private wake--just for family.</p>",
Artists = new Artist[]
{
new Artist() { Name = "Dan Slott", Role = "Script" },
new Artist() { Name = "Marcos Martin", Role = "Pencils" },
new Artist() { Name = "Marcos Martin", Role = "Inks" },
new Artist() { Name = "Muntsa Vicente", Role = "Colors" },
new Artist() { Name = "Joe Caramagna", Role = "Letters" }
},
Favorite = false
},
new ComicBook()
{
Id = 3,
Series = Series[1],
IssueNumber = 50,
DescriptionHtml = "<p><strong>The Dungeon & The Parapet, Part 1.</strong> Thorn is discovered by Lord Tarsil and the corrupted Stickeaters and thrown into a dungeon with Fone Bone. As she sleeps, a message comes to her about the mysterious \"Crown of Horns\".</p>",
Artists = new Artist[]
{
new Artist() { Name = "Jeff Smith", Role = "Script" },
new Artist() { Name = "Jeff Smith", Role = "Pencils" },
new Artist() { Name = "Jeff Smith", Role = "Inks" },
new Artist() { Name = "Jeff Smith", Role = "Letters" }
},
Favorite = false
}
};
Series = series;
ComicBooks = comicBooks;
}
}
}
1 Answer
James Churchill
Treehouse TeacherAdam,
When setting the ComicBook Series
property, you're referencing the Data Series
static property which hasn't been initialized yet.
new ComicBook()
{
Id = 1,
Series = Series[0],
...
So, try changing Series[0]
to series[0]
.
Thanks ~James
Adam Shockley
2,543 PointsAdam Shockley
2,543 PointsThanks for the quick response. I was pulling my hair out on this one.
James Churchill
Treehouse TeacherJames Churchill
Treehouse TeacherYou're welcome!