Click here to Skip to main content
16,004,458 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
It says that the reference was not set to an instance of an object when I hit the "create" button to create the virtual book. How can I get it to create a new book?

HTML
@model IEnumerable<openlibrary.models.author>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            Name
        </th>
        <th>
            Address1
        </th>
        <th>
            Email
        </th>
        <th>
            Phone
        </th>
        <th>
            DOB
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Address1)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Email)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Phone)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DOB)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.AuthorId }) |
            @Html.ActionLink("Details", "Details", new { id=item.AuthorId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.AuthorId })
        </td>
    </tr>
}

</table>




@model OpenLibrary.Models.Author

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Author</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Address1)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address1)
            @Html.ValidationMessageFor(model => model.Address1)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Email)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Email)
            @Html.ValidationMessageFor(model => model.Email)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Phone)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Phone)
            @Html.ValidationMessageFor(model => model.Phone)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DOB)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DOB)
            @Html.ValidationMessageFor(model => model.DOB)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div></openlibrary.models.author>
Posted
Updated 6-Feb-12 19:37pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Feb-12 2:51am    
Irrelevant code. Look, you tag it: "C#", so, show this C# code. Comment the line.
Actually, this is one of easiest bugs to fix.

1 solution

This is one of easiest bugs to fix: run it under the debugger. After exception, stop the running session, out a break point just before the line where exception was thrown and examine the values of the variables used in next statement. One of them will be null, and you are trying to call its instance method or access its instance property.

Find out which one. Fix it.

—SA
 
Share this answer
 
Comments
Abhinav S 7-Feb-12 3:03am    
Sage advice. 5.
Sergey Alexandrovich Kryukov 7-Feb-12 3:05am    
Thank you very much, Abhinav.
--SA
Mein Nam 8-Feb-12 0:21am    
The "Model" is null in
@foreach (var item in Model)
So what do I do?
Sergey Alexandrovich Kryukov 13-Feb-12 3:19am    
I don't know what should you do, I don't have enough information.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900