I'm a newbie to .NET and MVC, and for the first time I learned about this, suffering too long from ASP, and about the time I redid to simplify my work on building web applications.
I went through Stephen Walter with helpful video tutorials to cover most of the things, and so far I have made good progress. Where I got out, a record details page is created in my ConversationApp application. Listing data and inserting new data works fine, but every time I go to the details page for a specific record, I get the error message "Object reference not set to object instance."
URL passed to the controller: / Home / Details / X (where X is a unique identifier for the entry)
I would appreciate any help to get me past what is probably a very stupid mistake of mine or oversight.
Here, where I'm still, the code is wise:
HomeController.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ConversationApp.Models;
namespace ConversationApp.Controllers
{
public class HomeController : Controller
{
private conversationDBEntities _entities = new conversationDBEntities();
public ActionResult Index()
{
return View(_entities.conversation.ToList());
}
public ActionResult Details(int id)
{
return View();
}
public ActionResult Create()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Exclude="conversation_id")]conversation conversationToCreate)
{
try
{
_entities.AddToconversation(conversationToCreate);
_entities.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
public ActionResult Edit(int id)
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
}
Details.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ConversationApp.Models.conversation>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Details
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Details</h2>
<fieldset>
<legend>Fields</legend>
<p>
conversation_name:
<%= Html.Encode(Model.conversation_name) %>
</p>
<p>
conversation_owner:
<%= Html.Encode(Model.conversation_owner) %>
</p>
<p>
conversation_description:
<%= Html.Encode(Model.conversation_description) %>
</p>
<p>
conversation_homeurl:
<%= Html.Encode(Model.conversation_homeurl) %>
</p>
</fieldset>
<p>
<%=Html.ActionLink("Edit", "Edit", new { id=Model.conversation_id }) %> |
<%=Html.ActionLink("Back to List", "Index") %>
</p>
Error message for / Home / Details / X (where X is the unique identifier for the entry)
Server Error in '/' Application.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 13: <p>
Line 14: conversation_name:
Line 15: <%= Html.Encode(Model.conversation_name) %>
Line 16: </p>
Line 17: <p>
Source File: c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx Line: 15
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
ASP.views_home_details_aspx.__RenderContent2(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Home\Details.aspx:15
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
ASP.views_shared_site_master.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in c:\Users\Michael Harris\Documents\Visual Studio 2008\Projects\ConversationApp\ConversationApp\Views\Shared\Site.Master:26
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266
Version Information: Microsoft .NET Framework Version:2.0.50727.4918; ASP.NET
Version:2.0.50727.4918