Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

html - NullReference Exception in Blazors RenderTreeBuilder

So I'm working on a school project. I use blazor in the frontend and FluentValidation to validate forms.

First, the exception:

System.NullReferenceException
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=TemperBapes
  StackTrace:
   at TemperBapes.Pages.Register.BuildRenderTree(RenderTreeBuilder __builder)

I get the exception when calling the /Register page. I already had exceptions coming from the RenderTreeBuilder when my HTML was corrupt. I could always resolve the issues pretty fast so far. But I'm stuck with this one. Thats what my .razor file looks like:

@page "/Register"

<div class="card">
    <h5 class="card-header">Register</h5>
    <div class="card-body">
        <form>
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username" placeholder="Enter username" @bind-value="User.Username" />
                <ValidationMessage For="() => User.Username"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="displayname">Display Name</label>
                <input type="text" class="form-control" id="displayname" placeholder="Enter your display name" @bind-value="User.DisplayName" />
                <ValidationMessage For="() => User.DisplayName"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" @bind-value="User.EMail" />
                <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                <ValidationMessage For="() => User.EMail"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="tele">Tel</label>
                <input type="tel" class="form-control" id="tele" aria-describedby="telHelp" placeholder="Enter tel" @bind-value="User.Phonenumber" />
                <small id="telHelp" class="form-text text-muted">We'll never share your tel with anyone else.</small>
                <ValidationMessage For="() => User.Phonenumber"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="passwordfield">Password</label>
                <input type="password" class="form-control" id="passwordfield" placeholder="Password" @bind-value="User.Password" />
            </div>
            <button type="submit" class="btn btn-primary" @onclick="(() => SendRegister())">Submit</button>
        </form>
    </div>
</div>

@code{
    public AddUserDto User { get; set; }

    public void SendRegister()
    {
        
    }
}

The same exception gets thrown when the FluentValidation <ValidationMessage></ValidationMessage> is commented out, already tried that. Thank you all in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to initialize your User like

 public AddUserDto User { get; set; } = new AddUserDto();

Because your user is null, the bindings like @bind-value="User.Password" can't work and throws the error when used by the RenderTreeBuilder.

If you fix this issue, the page will render. However, you will run into the next issue.

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Microsoft.AspNetCore.Components.Forms.ValidationMessage`1[System.String] requires a cascading parameter of type EditContext. For example, you can use Microsoft.AspNetCore.Components.Forms.ValidationMessage`1[System.String] inside an EditForm.
System.InvalidOperationException: Microsoft.AspNetCore.Components.Forms.ValidationMessage`1[System.String] requires a cascading parameter of type EditContext. For example, you can use Microsoft.AspNetCore.Components.Forms.ValidationMessage`1[System.String] inside an EditForm.
   at Microsoft.AspNetCore.Components.Forms.ValidationMessage`1[[System.String, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].OnParametersSet()
   at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()

The message here is pretty straight forward. You need to add an EditContext. To do so, replace your <form> with a <EditForm> and add the Model Property.

 <EditForm Model="User">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" id="username" placeholder="Enter username" @bind-value="User.Username" />
                <ValidationMessage For="() => User.Username"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="displayname">Display Name</label>
                <input type="text" class="form-control" id="displayname" placeholder="Enter your display name" @bind-value="User.DisplayName" />
                <ValidationMessage For="() => User.DisplayName"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="email">Email address</label>
                <input type="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email" @bind-value="User.EMail" />
                <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
                <ValidationMessage For="() => User.EMail"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="tele">Tel</label>
                <input type="tel" class="form-control" id="tele" aria-describedby="telHelp" placeholder="Enter tel" @bind-value="User.Phonenumber" />
                <small id="telHelp" class="form-text text-muted">We'll never share your tel with anyone else.</small>
                <ValidationMessage For="() => User.Phonenumber"></ValidationMessage>
            </div>
            <div class="form-group">
                <label for="passwordfield">Password</label>
                <input type="password" class="form-control" id="passwordfield" placeholder="Password" @bind-value="User.Password" />
            </div>
            <button type="submit" class="btn btn-primary" @onclick="(() => SendRegister())">Submit</button>
 </EditForm>

To learn more about the EditForm, I recommend the following article.

https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-5.0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...