0

Please have a look at this image:

I want to loop through the "TopTens" im my view, We can see that there are 9 objects in "TopTens", why dooes it say: Object reference not set to an instance of an object?

![enter image description here][1]

[NullReferenceException: Object reference not set to an instance of an object.]
   ASP._Page_Views_secure_CreatePageDetailss_cshtml.Execute() in c:\Users\xxx\xxx\xxxx\xxxxx\xxxxx\Views\Secure\CreatePageDetailss.cshtml:18
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +271
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +120
   System.Web.WebPages.StartPage.RunPage() +63
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +100
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +131
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +695
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +382
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +431
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +39
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +116
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +529
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +106
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +321
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514928
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
user2915962
  • 2,691
  • 8
  • 33
  • 60
  • 2
    There are 9 items in that collection but are you sure one of them isn't null? (I'd post few lines of stack trace, with line numbers, too to find such kind of "problems") – Adriano Repetti Apr 27 '14 at 07:15
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – tom redfern Jan 11 '17 at 10:32

1 Answers1

4

There is only one explanation for the code you give us, and that is that one item in your list is null.

The statement @item.LongDesc will throw a NullReferenceException then.

If you allow one of the items in the TopTens to be null, add a check for null:

@if (@item != null)
{
  <p>@item.LongDesc</p>
}

Or, if TopTens is a List just don't add more items then there are, preventing one to be null.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thank you! I tried adding the if statement inside the foreach...But the breakpoint shows that the code doesnt even get passed the opening foreach-line before throwing an exception. It never even gets to the @item.LongDesc. – user2915962 Apr 27 '14 at 07:27
  • @user2915962: Can you post the stacktrace? – Patrick Hofman Apr 27 '14 at 07:28
  • @user2915962: And what is line 18? – Patrick Hofman Apr 27 '14 at 07:31
  • Im sorry, i dont understand much from the stacktrace...Does line 18 seems to be the problem? – user2915962 Apr 27 '14 at 07:35
  • @user2915962: That is the place the error is thrown. – Patrick Hofman Apr 27 '14 at 07:35
  • oh i get it, line 18 in the view is the loop: @foreach (var item in Model.TopTenContentPage.TopTens) – user2915962 Apr 27 '14 at 07:37
  • @user2915962: Then either one of the three (Model, TopTenContentPage, TopTens) is `null`. Are you sure you are looking at the last compiled code? – Patrick Hofman Apr 27 '14 at 07:40
  • Yes its the last compiled code, Ok thank you! I´ll look into the three models and see if I can find something thats null. Thank you for taking the time. – user2915962 Apr 27 '14 at 07:43
  • SOLVED I made a silly mistake...At the top of my page i used: model Models.TopTenContentPage... I just had to delete the "topcontentpage" from the loop and it worked fine! Now my @longDesc gets printed on the screen, A quick question though, can I use html.editfor in the loop to be able to edit the strings? Thanks! – user2915962 Apr 27 '14 at 07:59