aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Render NRE exception with lambda

Open mellamokb opened this issue 8 years ago • 2 comments

Continuing with the same example page I'm testing with, the filter code is causing an exception to be logged to console when attempting to perform a search.

@using BlazorPlayground.Shared
@using BlazorPlayground.Shared.Model
@page "/categories"
@inject HttpClient Http

@if (categories == null)
{
    <p><em>Loading...</em></p>
}
else
{
    <fieldset>
        <legend>Search</legend>
        <div class="form-horizontal">
            <div class="form-group-sm col-sm-6">
                <label class="control-label col-sm-4" for="Namefilter">Name</label>
                <div class="col-sm-8"><input class="form-control" type="text" id="NameFilter" name="NameFilter" bind="@NameFilter" /></div>
            </div>
            <div class="form-group-sm col-sm-6">
                <label class="control-label col-sm-4" for="DescriptionFilter">Description</label>
                <div class="col-sm-8"><input class="form-control" type="text" id="DescriptionFilter" name="DescriptionFilter" bind="@DescriptionFilter" /></div>
            </div>
        </div>
        <div class="col-offset-2"><button>Search</button></div>
    </fieldset>

    <table class="table">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Description</th>
                <th>Picture</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var category in GetCategories())
            {
                <tr>
                    <td><b>@category.CategoryID</b></td>
                    <td>@category.CategoryName</td>
                    <td>@category.Description</td>
                    <td><img src="@(string.Format("api/Category/Picture/{0}", category.CategoryID))" /></td>
                </tr>
            }
        </tbody>
    </table>

    <p>NameFilter: @NameFilter</p>
    <p>Description: @DescriptionFilter</p>
    <p>Categories: @categories.Length</p>
}

@functions {
    public string NameFilter { get; set; }
    public string DescriptionFilter { get; set; }
    public CategoryTransfer[] categories { get; set; }

    protected override async Task OnInitAsync()
    {
        categories = await Http.GetJsonAsync<CategoryTransfer[]>("/api/Category/All");
    }

    protected IEnumerable<CategoryTransfer> GetCategories()
    {
        Console.WriteLine("GetCategories()");
        var result = categories.AsQueryable();
        if (!string.IsNullOrWhiteSpace(NameFilter)) result = result.Where(x => x.CategoryName.IndexOf(NameFilter.Trim(), StringComparison.InvariantCultureIgnoreCase) >= 0);
        if (!string.IsNullOrWhiteSpace(DescriptionFilter)) result = result.Where(x => x.Description.IndexOf(DescriptionFilter.Trim(), StringComparison.InvariantCultureIgnoreCase) >= 0);
        return result;
    }
}

When I attempt to run this code, it loads fine initially. However when I type in a filter and it runs GetCategories again with either Name or Description Filter populated, the following exception is logged to console.

Uncaught Error: System.NullReferenceException: Object reference not set to an instance of an object.
  at System.Linq.Expressions.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType) <0x1badf88 + 0x00284> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.GetRunDelegateCtor (System.Type delegateType) <0x1ba79a8 + 0x00048> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.MakeDelegate (System.Type delegateType) <0x1ba7820 + 0x0000e> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate (System.Runtime.CompilerServices.IStrongBox[] closure) <0x1ba6f20 + 0x00024> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate () <0x1ba6db0 + 0x0000a> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile (System.Boolean preferInterpretation) <0x1f3a290 + 0x00018> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile () <0x1f3a258 + 0x0000a> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.EnumerableQuery`1[T].GetEnumerator () <0x1d13a48 + 0x00030> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.EnumerableQuery`1[T].System.Collections.Generic.IEnumerable<T>.GetEnumerator () <0x1d13438 + 0x00008> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at BlazorPlayground.Client.Pages.Categories.BuildRenderTree (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder) <0x1c97810 + 0x00988> in <f3511b7758e24314bdf26d3ca23eda2b>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19d87d0 + 0x0006c> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderQueueEntry renderQueueEntry) <0x19d8378 + 0x00040> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue () <0x19d59f0 + 0x00048> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.DispatchEvent (System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1f1e6c0 + 0x00068> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.DispatchBrowserEvent (System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1f1e558 + 0x00020> in <7bb47277895b48848360a8e56a0dcf02>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRendererEventDispatcher.DispatchEvent (System.String eventDescriptorJson, System.String eventArgsJson) <0x1d1b798 + 0x0005a> in <7bb47277895b48848360a8e56a0dcf02>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.MakeRunDelegateCtor (System.Type delegateType) <0x1badf88 + 0x00284> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.GetRunDelegateCtor (System.Type delegateType) <0x1ba79a8 + 0x00048> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightLambda.MakeDelegate (System.Type delegateType) <0x1ba7820 + 0x0000e> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate (System.Runtime.CompilerServices.IStrongBox[] closure) <0x1ba6f20 + 0x00024> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate () <0x1ba6db0 + 0x0000a> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile (System.Boolean preferInterpretation) <0x1f3a290 + 0x00018> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.Expressions.Expression`1[TDelegate].Compile () <0x1f3a258 + 0x0000a> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.EnumerableQuery`1[T].GetEnumerator () <0x1d13a48 + 0x00030> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at System.Linq.EnumerableQuery`1[T].System.Collections.Generic.IEnumerable<T>.GetEnumerator () <0x1d13438 + 0x00008> in <2f32b75d24f74a57ba299b78e971fb01>:0 
  at BlazorPlayground.Client.Pages.Categories.BuildRenderTree (Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder) <0x1c97810 + 0x00988> in <f3511b7758e24314bdf26d3ca23eda2b>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.ComponentState.RenderIntoBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderBatchBuilder batchBuilder, Microsoft.AspNetCore.Blazor.RenderFragment renderFragment) <0x19d87d0 + 0x0006c> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.RenderInExistingBatch (Microsoft.AspNetCore.Blazor.Rendering.RenderQueueEntry renderQueueEntry) <0x19d8378 + 0x00040> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.ProcessRenderQueue () <0x19d59f0 + 0x00048> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Rendering.Renderer.DispatchEvent (System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1f1e6c0 + 0x00068> in <90a6a0a0a09d4efabdb98ab543854719>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRenderer.DispatchBrowserEvent (System.Int32 componentId, System.Int32 eventHandlerId, Microsoft.AspNetCore.Blazor.UIEventArgs eventArgs) <0x1f1e558 + 0x00020> in <7bb47277895b48848360a8e56a0dcf02>:0 
  at Microsoft.AspNetCore.Blazor.Browser.Rendering.BrowserRendererEventDispatcher.DispatchEvent (System.String eventDescriptorJson, System.String eventArgsJson) <0x1d1b798 + 0x0005a> in <7bb47277895b48848360a8e56a0dcf02>:0 
    at Object.callMethod (MonoPlatform.ts:70)
    at raiseEvent (BrowserRenderer.ts:304)
    at EventDelegator.onEvent (BrowserRenderer.ts:17)
    at EventDelegator.onGlobalEvent (EventDelegator.ts:77)

I've also verified that this code works correctly if I put it in the Server-side in a controller and call it as an API, instead of embedded in a Razor page. So the code should be functional.

mellamokb avatar Apr 21 '18 02:04 mellamokb

I expect you’ve found some edge case in Mono’s Linq expression interpreter that doesn’t work correctly. When we’ve reported these to the Mono project in the past they’ve generally been fixed quickly.

Would you be able to report this to https://github.com/mono/mono? To be sure it’s not a Blazor issue, please try to reproduce the issue by running your code directly inside Program.cs first, rather than in a Blazor component.

SteveSandersonMS avatar Apr 21 '18 13:04 SteveSandersonMS

The code works fine in Program.cs, so I guess it must be an issue in mono. Thanks!

mellamokb avatar Apr 28 '18 16:04 mellamokb