简体   繁体   中英

create user wizard - null reference exception was unhandled by user code

I'm new to ASP.NET. I have a createUserWiazrd named RegisterUserWithRoles taken from Step 4 of this tutorial http://www.asp.net/web-forms/tutorials/security/roles/assigning-roles-to-users-cs

Here is the aspx File:

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"    
 CodeFile="CreateUsers.aspx.cs" Inherits="Membership_CreateUser" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<h2>
    Create Users</h2>
<p>
    <asp:CreateUserWizard ID="RegisterWithRoles" runat="server" 
        ContinueDestinationPageUrl="~/Default.aspx" LoginCreatedUser="False" 
        onactivestepchanged="RegisterWithRoles_ActiveStepChanged">
        <WizardSteps>
            <asp:CreateUserWizardStep runat="server" />

            <asp:WizardStep ID="SpecifyRoles" runat="server" AllowReturn="False" 
                StepType="Step" Title="Specify Roles">
                <asp:CheckBoxList ID="RoleList" runat="server">
                </asp:CheckBoxList>
            </asp:WizardStep>

            <asp:CompleteWizardStep runat="server" />
        </WizardSteps>
    </asp:CreateUserWizard>
</p>
<p>

</p>
</asp:Content>

and Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class Membership_CreateUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)       
    {            
        // Reference the SpecifyRolesStep WizardStep
        WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRolesStep") as     
WizardStep;

        // Reference the RoleList CheckBoxList            
        CheckBoxList RoleList = SpecifyRolesStep.FindControl("RoleList") as CheckBoxList;

        // Bind the set of roles to RoleList            
        RoleList.DataSource = Roles.GetAllRoles();            
        RoleList.DataBind();       
    }  
}



protected void RegisterWithRoles_ActiveStepChanged(object sender, EventArgs e)
{
    // Have we JUST reached the Complete step?       
    if (RegisterWithRoles.ActiveStep.Title == "Complete")
    {
        // Reference the SpecifyRolesStep WizardStep            
        WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRoles") as 
WizardStep;

        // Reference the RoleList CheckBoxList            
        CheckBoxList RoleList = SpecifyRolesStep.FindControl("RoleList") as CheckBoxList;

        // Add the checked roles to the just-added user            
        foreach (ListItem li in RoleList.Items)
        {
            if (li.Selected)
                Roles.AddUserToRole(RegisterWithRoles.UserName, li.Text);
        }
    } 
}
}

I keep getting the error

null reference exception was unhandled by user code - Object reference not set to an instance of an object.

There are five roles, I checked using the ASP.NET Configuration . Can you please help me understand the origin of this error.

Thanks in advance!

Although I don't use the provided wizards for this functionality, and a stack trace would be useful, I'm reasonably confident the problem is here-

WizardStep SpecifyRolesStep = RegisterWithRoles.FindControl("SpecifyRolesStep") as WizardStep;

The FindControl ID you are selecting SpecifyRolesStep does not match the ID in your ASPX file-

<asp:WizardStep ID="SpecifyRoles" runat="server" AllowReturn="False" 
            StepType="Step" Title="Specify Roles">

The "as" keyword (as opposed to simply casting using the syntax (WizardStep)RegisterWithRoles.FindControl("SpecifyRolesStep") ) should mean that SpecifyRolesStep is safely set to null even if the FindControl doesn't return an object or the object is an incompatible type, so it is good practice to use this "as" keyword where possible (as you have here) and then check for nulls before manipulating the resulting objects using the following syntax-

if (RoleList != null)
{ 
  //code working with RoleList object
}
else
{ 
   //handle missing control
}

Of course it is not always possible to continue the program flow where the object is null, it may be critical to the functionality, so you may not be able to handle the error beyond throwing an ApplicationException with a more specific error message to help debugging.

In this case you obviously have to make a judgement as a developer on taking the time to check for nulls all the time (and clutter your code) when all you are going to do is throw an exception anyway.

Background aside, you either need to change the FindControl parameter you are using for SpecifyRolesStep or rename the WizardStep "SpecifyRoles" in your ASPX to match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM