-1

I am new to C# and asp .net MVC please help. i have tried to use partial views for creating a form aswell as 3 lists which will show notice and cards but every time i tried to run it it shows null exception. i dont know how to fix this error please help

'''

Server Error in '/' Application.
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.

Source Error:


Line 6:      //if (Model != null)
Line 7:      //{
Line 8:      foreach (var item in Model)
Line 9:      {
Line 10:         <tr>


Source File: C:\Users\Koustav Shee\Desktop\ProjectLastSupper\Views\Shared\_IndexNotice.cshtml    Line: 8

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   ASP._Page_Views_Shared__IndexNotice_cshtml.Execute() in C:\Users\Koustav Shee\Desktop\ProjectLastSupper\Views\Shared\_IndexNotice.cshtml:8
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +90
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.HtmlHelper.RenderPartialInternal(String partialViewName, ViewDataDictionary viewData, Object model, TextWriter writer, ViewEngineCollection viewEngineCollection) +277
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName, Object model, ViewDataDictionary viewData) +91
   System.Web.Mvc.Html.PartialExtensions.Partial(HtmlHelper htmlHelper, String partialViewName) +32
   ASP._Page_Views_Home_Index_cshtml.Execute() in C:\Users\Koustav Shee\Desktop\ProjectLastSupper\Views\Home\Index.cshtml:18
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9850009
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.8.4330.0 '''

For controller i have used this codes public class HomeController : Controller { Entities db = new Entities();

        [AllowAnonymous]
        public ActionResult Index()
        {
            IndexAIO1 model = new IndexAIO1();

            
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult IndexAntiRagging([Bind(Exclude = "timestamp,is_resolved")] AntiRag Ragg)
        {
            var message = "";
            bool status = false;
            if (ModelState.IsValid)
            {
                Ragg.is_resolved = false;
                Ragg.timestamp = System.DateTime.Now;

                using(Entities dc = new Entities())
                {
                    dc.AntiRags.Add(Ragg);
                    dc.SaveChanges();
                    status = true;
                    message = "Complaint sent Successfully!";
                }
            }
            else
            {
                message = "invalid Request!";
            }
            ViewBag.Message = message;
            ViewBag.Status = status;
            return View("Index");
        }

        [HttpGet]
        
        public ActionResult IndexBlog()
        {
            Entities dc = new Entities();
            return View(dc.Blogs.ToList());
        }

        [HttpGet]
        public ActionResult IndexDiscover()
        {
            Entities dc = new Entities();
            return View(dc.Discovers.ToList());
        }
        
        public ActionResult IndexNotice()
        {
            List<Notice> notices;

            notices = db.Notices.Where(x => x.IsActive.Equals(true)).ToList();
            return View(notices);
        }

my view page

@{
    ViewBag.Title = "Home";
}
@section StyleSheet{

    <link href="~/assets/CSS/fontawesome.min.css" rel="stylesheet" />
    <link href="~/assets/CSS/index.css" rel="stylesheet" />
    <link href="~/assets/CSS/slick-theme.css" rel="stylesheet" />
    <link href="~/assets/CSS/slick.css" rel="stylesheet" />
    <script src="~/assets/JavaScript/fontawesome.js"></script>
    <script src="~/assets/JavaScript/slick.min.js"></script>
    <script src="~/assets/JavaScript/main.js"></script>
}
<section class="notice">
    <table id="notice-container">
        
        @Html.Partial("_IndexNotice")
    </table>
</section>
<!--===================================| IMAGE BAR [section 2] |=================================-->

<section class="sec3">
    <div class=" slider ">
        <div class="slides ">
            <input type="radio" name="radio-btn " id="radio1">
            <input type="radio" name="radio-btn " id="radio2">
            <input type="radio" name="radio-btn " id="radio3">
            <input type="radio" name="radio-btn " id="radio4">
            <input type="radio" name="radio-btn " id="radio5">
            <div class="slide first ">
                <img src="~/assets/Images/clg_1.jpg " alt=" ">
            </div>
            <div class="slide ">
                <img src="~/assets/Images/clg_2.jpg" alt=" ">
            </div>
            <div class="slide ">
                <img src="~/assets/Images/clg_3.jpg" alt=" ">
            </div>
            <div class="slide ">
                <img src="~/assets/Images/clg_4.jpg" alt=" ">
            </div>
            <div class="slide ">
                <img src="~/assets/Images/clg_5.jpg" alt=" ">
            </div>
            <div class="navigation-auto ">
                <div class="auto-btn1 "></div>
                <div class="auto-btn2 "></div>
                <div class="auto-btn3 "></div>
                <div class="auto-btn4 "></div>
                <div class="auto-btn5 "></div>
            </div>
        </div>
        <div class="navigation-manual ">
            <label for="radio1" class="manual-btn"></label>
            <label for="radio2" class="manual-btn"></label>
            <label for="radio3" class="manual-btn"></label>
            <label for="radio4" class="manual-btn"></label>
            <label for="radio5" class="manual-btn"></label>
        </div>
    </div>
</section>
<!--======================================| INTRO [section 3] |==================================-->

<section class="intro">
    <div class="int1">
        <div class="int2">
            <p class=" int-para ">
                <strong>Welcome To!<strong> 
            </p>
        </div>
    </div>
</section>
<!--======================================| CARD [section 4] |===================================-->

<section class="sec4">
    <h2 class="head">EVENTS & BLOGS</h2>
    <div class="swiper-container" style="margin:auto;">
        <div class="card carousel" id="EventContainer">
            @Html.Partial("_IndexBlog")
        </div>
    </div>
</section>

<!--======================================| CARD [section 5] |===================================-->
<hr>
<section class="sec5 ">
    <h2 class="head">DISCOVER</h2>
    <div class="swiper-container" style="margin:auto;">
        <div class="card carousel" id="DiscoverContainer">
            @Html.Partial("_IndexDiscover")
        </div>
    </div>
</section>
<hr>

<!--==================================| Anti-Ragging Greviance |=================================-->
<section class="anti_ragg">
    @Html.Partial("_IndexAntiRagging")
</section>

@section scripts{
    <script src="~/assets/JavaScript/index.js"></script>
}

my partial view: '''

@model IEnumerable<ProjectLastSupper.Models.Notice>


@using (Html.BeginForm("IndexNotice", "Home"))
{
    //if (Model != null)
    //{
    foreach (var item in Model)
    {
        <tr>
            <th class="ntc">@Html.DisplayFor(model => item.N_type) NOTICE:</th>
            <th>
                <marquee id="note">@Html.DisplayFor(model => item.subject)</marquee>
            </th>
        </tr>
    }
}
H.Sarxha
  • 63
  • 1
  • 8
  • [How do I write a good title](https://meta.stackexchange.com/q/10647/299295). – Sinatr May 06 '21 at 07:54
  • 4
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun May 06 '21 at 07:59
  • The error tells you the issue, somewhere there is an object that is null that shouldn't be – JamesS May 06 '21 at 08:25

0 Answers0