No matter how I flip this script around it still gives me Object reference not set to an instance of an object. message when I debug (The program runs fine if I don’t debug, but every time I debug I get this message, why is that?)
public string GetTemplate(int pageId)
{
string template = "";
PagesIndex qq = new PagesIndex();
qq = (from cc in db.PagesIndexes where cc.Id == pageId select cc).SingleOrDefault();
if (qq.Template == null) //Here on qq.Template - Object reference not set to an instance of an object.
{
template = "_Layout";
}
else
{
template = qq.Template;
}
return template;
}
Hope you can change my mood 🙂
Probably
qqresulting from LINQ is null.Try this
or shortly: