Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core NullReferenceException when just accessing model

I am having trouble with attempting to create a view with a strongly typed model. No matter what I pass in as the model to a View(), I always receive a NullReferenceException when even just accessing the Model.

I can't even check if the model is null before executing the rest of the razor page; simply doing a if (Model != null) also throws the same NullReferenceException.

Index.cshtml

@page
@model EncodeModel
@{
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h2>Encode</h2>

<div id="progress">
    @await Html.PartialAsync("~/Encoder/MVC/EncodeProgress.cshtml", new EncodeModule())
</div>

EncodeProgress.cshtml

@page
@model FFenc.IEncoderModule

@{
    var module = Model; //this throws the NullReferenceException
}

Startup.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseMvc();
    }

Exception stack trace:

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

AspNetCore.Encoder_MVC_EncodeProgress.get_Model()
AspNetCore.Encoder_MVC_EncodeProgress.ExecuteAsync() in EncodeProgress.cshtml
var module = Model;

What am I doing wrong? I have attempted multiple fixes and workarounds (using a ViewComponent instead of a view) but nothing works.

Some similar questions that I have found that have not solved my problem:

ASP.NET Core Model null error in the Index view

I'm already passing in the model so this answer doesn't change anything about what I'm doing. For example, when I was trying to use a controller as a workaround, the same NullReferenceException happened with this code:

    [Route("/encode/progress")]
    public IActionResult GetProgress()
    {
        return View("~/Encoder/MVC/EncodeProgress.cshtml", new EncoderModule());
    }
like image 677
user5860357 Avatar asked Aug 25 '18 03:08

user5860357


People also ask

Why am I getting a NullReferenceException?

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference, and the reference is not initialized (or it was once initialized, but is no longer initialized). This means the reference is null , and you cannot access members (such as methods) through a null reference.

How do I fix NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

What does system NullReferenceException mean?

A NullReferenceException exception is thrown when you try to access a member on a type whose value is null . A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios: You've forgotten to instantiate a reference type.


1 Answers

I think you're mixing Razor Pages with ASP.NET MVC. Try removing the @page directive and your model should be bound to the value passed when you call return View().

like image 123
Justin Helgerson Avatar answered Oct 12 '22 13:10

Justin Helgerson