2013-03-09 19 views
7

Dostaję dziwny błąd znajduje się wOdwołanie do obiektu nie jest ustawione na wystąpienie obiektu. w MVC Layout = null

@{ 
    Layout = null; 
} 

Jest to błąd:

Odwołanie do obiektu nie zostało ustawione na wystąpienie obiektu.

Opis: Nieobsługiwany wyjątek wystąpił podczas wykonywania bieżącego żądania WWW. Zapoznaj się ze śledzeniem stosu, aby uzyskać więcej informacji o błędzie i miejscu jego powstania w kodzie.

Szczegóły wyjątku: System.NullReferenceException: Obiekt odniesienia nie ustawiony na wystąpienie obiektu.

I ślad stosu:

[NullReferenceException: Object reference not set to an instance of an object.]  
ASP._Page_Views_Home_Index_cshtml.Execute() in f:\Web Prog\my work\mcpd\mvc\FilippoPhotography\FP.WebUI\Views\Home\Index.cshtml:4  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197  
System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +97  
System.Web.WebPages.StartPage.RunPage() +17  
System.Web.WebPages.StartPage.ExecutePageHierarchy() +62  
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76  
System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +260  
System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +115  
System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +295  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13  
System.Web.Mvc.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17() +23  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +242 
System.Web.Mvc.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19() +21  
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +177  
System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +89  
System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102  
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +57  
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43  
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14  
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23  
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62  
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23  
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62  
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47  
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10  
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25  
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23  
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62  
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47  
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9  
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296 
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

Oto mój kontroler z tym poglądem:

namespace FP.WebUI.Controllers 
{ 
    public class HomeController : Controller 
    { 
     private IInfoRepo repo; 
     public HomeController(IInfoRepo repoParam) 
     { 
      repo = repoParam; 
     } 
     public ViewResult Index() 
     { 
      Info model = repo.Info.FirstOrDefault(); 
      return View(model); 
     } 
    } 
} 

Oto sam widok:

@model FP.Domain.Entities.Info 

@{ 
    Layout = null; 
} 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>A.Filippo Photography</title> 
    <link rel="Stylesheet" type="text/css" href="~/Content/css/whole.css" /> 
    <link rel="Stylesheet" type="text/css" href="~/Content/css/Homepage.css" /> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script src="~/Content/js/homepage.js" type="text/javascript"></script> 
</head> 
<body> 
    <div id="whole_wrapper"> 
     <header> 
      <div id="follow"> 
       <a href="http://@Model.Facebook.Substring(Model.Facebook.IndexOf("http://")+1,Model.Facebook.Length)"><img src="~/Content/img/temp.png" alt="facebook"/></a> 
       <a href="http://@Model.Twitter.Substring(Model.Twitter.IndexOf("http://")+1,Model.Twitter.Length)"><img src="~/Content/img/temp.png" alt="twitter"/></a> 
       <a href="mailto://@Model.Email"><img src="~/Content/img/temp.png" alt="email"/></a> 
      </div> 
      <div id="iconlabel"> 
       <a href="~/Views/Home/Index"><img src="~/Content/img/logo.png" alt="logo"/></a> 
      </div> 
     </header> 

     <div id="main_content"> 
       <div id="slides_wrapper"> 
        <a href="gallery.aspx"> 
         <div id="gallery_slide">       
          <p>Gallery</p> 
         </div> 
        </a> 
        <a href="sessions.aspx"> 
         <div id="session_slide"> 
          <p>Sessions</p> 
         </div> 
        </a> 
        <a href="offers.aspx"> 
         <div id="offers_slide"> 
          <p>Offers</p> 
         </div> 
        </a> 
        <a href="about.aspx"> 
         <div id="about_slide"> 
          <p>About Us</p> 
         </div> 
        </a> 
        <a href="contact.aspx"> 
         <div id="contact_slide"> 
          <p>Contact Us</p> 
         </div> 
        </a> 
       </div>     
      </div> 

      <div id="footer"> 
       <div id="copyright"> 
        <div id="rights"> 
         <p>All rights reserved.© - Best viewed using the latest version of <a href="http://www.google.com/chrome">Google Chrome</a> or <a href="www.mozilla.org/en-US/firefox/new/">Mozilla Firefox</a>.</p>      
        </div> 
        <div id="stamp">      
         <p>Crafted by</p> 
         <a href="http://about.me/rafael.adel">Rafael Adel</a> 
        </div>     
       </div> 
      </div> 

    </div> 
</body> 
</html> 

Każda pomoc będzie mile widziane, denerwuję się tutaj. Dzięki.

+0

Podaj pełny kod tutaj .. Jestem pewien, że używasz 'Układu' gdziekolwiek indziej .. –

+0

Jaki jest twój 'Index.cshtml', linia 4 wygląda? – andri

+0

Zaktualizowano pytanie. Dzięki. –

Odpowiedz

1

Możliwe linie wyjątek:

<a href="http://@Model.Facebook.Substring(Model.Facebook.IndexOf("http://")+1,Model.Facebook.Length)"><img src="~/Content/img/temp.png" alt="facebook"/></a> 
<a href="http://@Model.Twitter.Substring(Model.Twitter.IndexOf("http://")+1,Model.Twitter.Length)"><img src="~/Content/img/temp.png" alt="twitter"/></a> 

Prawdopodobnie kod Info model = repo.Info.FirstOrDefault(); zwraca domyślne, co oznacza, nieważne i wywołanie innej metody jak Substring() NULL powoduje wyjątek.