Click here to Skip to main content
16,019,983 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I'm new to C# and i've been stuck on this for hours. I tried multiple passings (ViewBag, ViewData and also as a parameter). In my View it says it gets a NullPointerException at the foreach loop.

What I have tried:

this is my Model:
namespace SoPro3.Models
{
    public class BookingModel
    {
        public int ladestand { get; set; }
        public int fahrstrecke { get; set; }
        public DateTime startzeit { get; set; }
        public DateTime endzeit { get; set; }

    }
}



this is my Controller:
namespace SoPro3.Controllers
{
    public class BookingController : Controller
    {
        //  
        // GET: /BookingModel/
        public ActionResult Index()
        {
            List<BookingModel> bookings = new List<BookingModel>() {
                new BookingModel
                {
                    ladestand = 10,
                    fahrstrecke = 100,
                    startzeit = new DateTime(2020, 10, 2, 8, 45, 0),
                    endzeit = new DateTime(2020, 10, 2, 12, 45, 0)
                },
                new BookingModel
                {
                    ladestand = 65,
                    fahrstrecke = 340,
                    startzeit = new DateTime(2020, 05, 3, 9, 45, 0),
                    endzeit = new DateTime(2020, 05, 3, 11, 45, 0)
                },
                new BookingModel
                {
                    ladestand = 30,
                    fahrstrecke = 150,
                    startzeit = new DateTime(2020, 09, 15, 22, 45, 0),
                    endzeit = new DateTime(2020, 09, 16, 0, 00, 0)
                }
                };

            return View(bookings);
        }
    }
}



This is my View:
@model List<SoPro3.Models.BookingModel>

@{
    ViewBag.Title = "Index";
}

<!DOCTYPE html>

<html>
<head lang="en">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Bookings</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>

    <h1 class="display-4">Bookings</h1>
    <div class="text-center">
            <table>
                <tr>
                    <th>Ladestand</th>
                    <th>Fahrstrecke</th>
                    <th>Startzeit</th>
                    <th>Endzeit</th>
                </tr>

                @foreach (var item in Model)
                {
                    <tr>
                        <th>@item.ladestand</th>
                        <th>@item.fahrstrecke</th>
                        <th>@item.startzeit</th>
                        <th>@item.endzeit</th>
                    </tr>
                }
            </table>
        }
    </div>
</body>
</html>



I tried a few things like changing the model to IEnumeration<t> but it seems not to work aswell.
Posted
Updated 5-May-20 3:18am
Comments
Richard MacCutchan 4-May-20 15:25pm    
Where does the NullPointerException occur, and which reference variable is the one that is null?
Todor Iliev 5-May-20 8:16am    
As i said it occurs in the foreach loop in my View code. There is only one foreach loop "@foreach (var item in Model)" in line 27, specifically highliting "Model".
Hier is the trace, dont know if it helps:

NullReferenceException: Object reference not set to an instance of an object.
AspNetCore.Views_Home_Index.<executeasync>b__13_1() in Index.cshtml
-
Ladestand Fahrstrecke Startzeit Endzeit @foreach (var item in Model)
{
@item.ladestand @item.fahrstrecke @item.startzeit @item.endzeitMicrosoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCore.Views_Home_Index.ExecuteAsync() in Index.cshtml
+
ViewBag.Title = "Index";
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<invokenextresultfilterasync>g__Awaited|29_0<tfilter, tfilterasync="">(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext<tfilter, tfilterasync="">(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<invokenextresourcefilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, object state, bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<invokeasync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
Microsoft.AspNetCore.Routing.EndpointMiddleware.<invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
Richard MacCutchan 5-May-20 8:42am    
Yes but which reference item is null?
Todor Iliev 5-May-20 8:47am    
the 'Model' which should contain the items
Richard MacCutchan 5-May-20 8:59am    
Where do you initialise the Model variable?

1 solution

As mentioned in comments, after checking out structure of site found that there was an Index view causing issue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900