Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm getting a strange error located in

@{
    Layout = null;
}

This is the error :

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.

And the stack trace :

[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

Here's my controller of this view :

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

Here's the view itself :

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

Any help would be appreciated, i'm getting frustrated here. Thanks.

like image 747
Rafael Adel Avatar asked Mar 09 '13 14:03

Rafael Adel


1 Answers

Possible exception lines:

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

Possibly the code Info model = repo.Info.FirstOrDefault(); returns a default which means null and calling any method like Substring() with null causes an exception.

like image 65
Kadir.K Avatar answered Nov 15 '22 01:11

Kadir.K