question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Binding nullable int to input causes null reference exception

See original GitHub issue

Minimal repro steps

Consider this Blazor page:

<input @bind(TestInt) />

@functions
{
    public int? TestInt { get; set; }

}

Expected result

Page should render without exceptions

Actual result

A nullref exception is thrown:

SCRIPT5022: System.NullReferenceException: Object reference not set to an instance of an object.
  at Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder.AddAttribute (System.Int32 sequence, System.String name, System.Object value) <0x169c870 + 0x0005c> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Datack.Blazor.Pages.Data.Index.BuildRenderTree (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder) <0x1977930 + 0x000dc> in <cd4fad1f0c094e678f0f1612015bbf56>:0 
  at (wrapper delegate-invoke) <Module>.invoke_void_RenderTreeBuilder(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder)
  at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x16964f8 + 0x0006c> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderQueueEntry renderQueueEntry) <0x16960a0 + 0x00040> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue () <0x16931b8 + 0x00048> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.AddToRenderQueue (System.Int32 componentId, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x16923d0 + 0x00068> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Components.RenderHandle.Render (Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x16920b8 + 0x00036> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Routing.Router.Refresh () <0x17672a0 + 0x00130> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at Microsoft.AspNetCore.Blazor.Routing.Router.OnLocationChanged (System.Object sender, System.String newAbsoluteUri) <0x1974c90 + 0x0002c> in <6f55c2fa30114054ab77a67cb3b359ec>:0 
  at (wrapper delegate-invoke) System.EventHandler`1[System.String].invoke_void_object_TEventArgs(object,string)
  at (wrapper delegate-invoke) System.EventHandler`1[System.String].invoke_void_object_TEventArgs(object,string)
  at Microsoft.AspNetCore.Blazor.Browser.Services.BrowserUriHelper.NotifyLocationChanged (System.String newAbsoluteUri) <0x1974b00 + 0x00026> in <aa678c00565443a1a03daf9fa7da0de1>:0 

Further technical details

Build: 0.1 OS: Windows 10 Pro Browser: Edge

Making the nullable int non-nullable wil fix this. So this does work as expected:

<input @bind(TestInt) />

@functions
{
    public int TestInt { get; set; }

}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
haaseycommented, Nov 26, 2018

I feel like this is a hole. If someone uses entity framework to bind to a database, it will force them to create new entities taking care of nulls. Would love to see some configuration that allows treating nulls as “” for strings and nulls as 0 for ints. I don’t want to get into a philosophical discussion on why they aren’t equal. In my mind I realize that but the application has to deal with it somehow, and I would prefer to handle it globally not field by field.

If I have a nullable string in a database and I want to bind using blazor I don’t like having to do some type of extension method to solve this over and over. public static string NoNull(this string value) { string rc = “”; if (value != null) { rc = value; } return rc; }

0reactions
LaughingJohncommented, Jun 10, 2019

“I don’t want to get into a philosophical discussion” I think that’s unavoidable when dealing with nulls, especially true of numbers in a financial system- zero returns is very different to no returns. That said, I agree completely that the ability to bind to nullable fields would be helpful but I’d want my nulls preserved on any round trip, not turned into empty strings/zeros. Something along the lines of TargetNullValue or IValueConverter would be very helpful, especially as a lot of people will be coming to this from a WPF/SL background.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
A nullable reference type is noted using the same syntax as nullable value types: a ? is appended to the type of the...
Read more >
Fighting with nullable reference types in Razor Pages
Nullable reference types were introduced in C# 7.3 as an attempt to work around the "billion dollar problem", null as the default for...
Read more >
Working with nullable reference types - EF Core
In this article. C# 8 introduced a new feature called nullable reference types (NRT), allowing reference types to be annotated, indicating ...
Read more >
Null Reference Exceptions
Null Reference Exceptions. A NullReferenceException happens when you try to access a reference variable that isn't referencing any object.
Read more >
Binding to a null string Value throws NullReferenceException
When the Value you bind to the DropDownList is null (for example, because it is a model that is not filled in by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found