关于c#:对象引用未设置为对象的实例。

Object reference not set to an instance of an object. in MVC Layout = null

本问题已经有最佳答案,请猛点这里访问。

我发现一个奇怪的错误

1
2
3
@{
    Layout = null;
}

这是错误:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.

以及堆栈跟踪:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[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

这是这个视图的控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
        }
    }
}

这是视图本身:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@model FP.Domain.Entities.Info

@{
    Layout = null;
}

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    A.Filippo Photography
    <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 src="~/Content/js/homepage.js" type="text/javascript">
</head>
<body>
   
        <header>
           
                <img src="~/Content/img/temp.png" alt="facebook"/>
                <img src="~/Content/img/temp.png" alt="twitter"/>
                <img src="~/Content/img/temp.png" alt="email"/>
           
           
                <img src="~/Content/img/logo.png" alt="logo"/>
           
        </header>

       
               
                   
                                               
                            <p>
Gallery
</p>
                       
                   
                   
                       
                            <p>
Sessions
</p>
                       
                   
                   
                       
                            <p>
Offers
</p>
                       
                   
                   
                       
                            <p>
About Us
</p>
                       
                   
                   
                       
                            <p>
Contact Us
</p>
                       
                   
                               
           

           
               
                   
                        <p>
All rights reserved.? - Best viewed using the latest version of Google Chrome or Mozilla Firefox.
</p>                    
                   
                                       
                        <p>
Crafted by
</p>
                        Rafael Adel
                                       
               
           

       
</body>
</html>

任何帮助都会得到感激,我在这里感到沮丧。谢谢。


可能的例外行:

1
2
<img src="~/Content/img/temp.png" alt="facebook"/>
<img src="~/Content/img/temp.png" alt="twitter"/>

代码Info model = repo.Info.FirstOrDefault();可能返回一个默认值,这意味着空值,调用任何方法(如Substring()的空值)都会导致异常。