2013-03-09 44 views
7

我得到位於未將對象引用設置爲對象的實例。在MVC佈局= NULL

@{ 
    Layout = null; 
} 

這是一個奇怪的錯誤就是錯誤:未設置爲一個對象的實例

對象引用。

描述:在執行 當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.NullReferenceException:對象引用不是 設置爲對象的實例。

而且堆棧跟蹤:

[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 

這裏是我這一觀點的控制器:

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); 
     } 
    } 
} 

這裏的視圖本身:

@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> 

任何幫助,將不勝感激,我在這裏感到沮喪。謝謝。

+0

把你的完整代碼放在這裏..我確定你在其他地方使用你的'Layout' .. – 2013-03-09 14:57:24

+0

你的'Index.cshtml',第4行看起來像什麼? – andri 2013-03-09 14:58:18

+0

更新了問題。謝謝。 – 2013-03-09 14:59:10

回答

1

可能的例外線:

<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> 

可能的代碼Info model = repo.Info.FirstOrDefault();返回缺省這意味着空並調用等Substring()具有空的任何方法導致異常。

相關問題