Solved

ASP .Net MVC: NullReferenceException was unhandled by user code

Posted on 2011-02-22
2
1,631 Views
Last Modified: 2013-12-17
I am having the following error in the forech of the code below (I am using Entity Framework for the model):
NullReferenceException was unhandled
Use the "new" keyword to create an object instance.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Project>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <table>
        <tr>
            <th></th>
            <th>
                ProjectID
            </th>
            <th>
                ProjectNumber
            </th>
            <th>
                ProjectTitle
            </th>
            <th>
                StartDate
            </th>
            <th>
                EndDate
            </th>
            <th>
                MinLatitude
            </th>
            <th>
                MaxLatitude
            </th>
            <th>
                MinLongitude
            </th>
            <th>
                MaxLongitude
            </th>
            <th>
                MinAltitude
            </th>
            <th>
                MaxAltitude
            </th>
            <th>
                ObjectiveCode
            </th>
            <th>
                TracerID
            </th>
            <th>
                TracerInfo
            </th>
            <th>
                AnalyticalErrors
            </th>
            <th>
                ProjectReferences
            </th>
            <th>
                SummaryInfo
            </th>
            <th>
                TCProjectNumber
            </th>
            <th>
                DistributionRestricted
            </th>
            <th>
                CheckStatus
            </th>
            <th>
                Complete
            </th>
            <th>
                DateEntered
            </th>
            <th>
                DateChanged
            </th>
            <th>
                DateExported
            </th>
        </tr>

    <% foreach (var item in Model) { %>
    
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.ProjectID }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.ProjectID })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.ProjectID })%>
            </td>
            <td>
                <%: item.ProjectID %>
            </td>
            <td>
                <%: item.ProjectNumber %>
            </td>
            <td>
                <%: item.ProjectTitle %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.StartDate) %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.EndDate) %>
            </td>
            <td>
                <%: item.MinLatitude %>
            </td>
            <td>
                <%: item.MaxLatitude %>
            </td>
            <td>
                <%: item.MinLongitude %>
            </td>
            <td>
                <%: item.MaxLongitude %>
            </td>
            <td>
                <%: item.MinAltitude %>
            </td>
            <td>
                <%: item.MaxAltitude %>
            </td>
            <td>
                <%: item.ObjectiveCode %>
            </td>
            <td>
                <%: item.TracerID %>
            </td>
            <td>
                <%: item.TracerInfo %>
            </td>
            <td>
                <%: item.AnalyticalErrors %>
            </td>
            <td>
                <%: item.ProjectReferences %>
            </td>
            <td>
                <%: item.SummaryInfo %>
            </td>
            <td>
                <%: item.TCProjectNumber %>
            </td>
            <td>
                <%: item.DistributionRestricted %>
            </td>
            <td>
                <%: item.CheckStatus %>
            </td>
            <td>
                <%: item.Complete %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.DateEntered) %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.DateChanged) %>
            </td>
            <td>
                <%: String.Format("{0:g}", item.DateExported) %>
            </td>
        </tr>
    
    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>

</asp:Content>

Open in new window

0
Comment
Question by:DistillingExperts
2 Comments
 
LVL 6

Accepted Solution

by:
ViceroyFizzlebottom earned 500 total points
ID: 34953518
You don't show the controller code, so I'll ask the obvious question as to whether you are instantiating your model and checking for null before you return your View from the controller?
0
 

Author Comment

by:DistillingExperts
ID: 34959348
I recreate the project and now I got another error that I already posted.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now