2012-12-07 2 views
2

ASP.NET을 처음 사용했습니다.사용자 만들기 마법사 - null 참조 예외가 사용자 코드에 의해 처리되지 않았습니다.

<%@ 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> 

및 코드 뒤에 :

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); 
     } 
    } 
} 
} 

나는 오류

가 계속 나는 영문 파일이 튜토리얼 여기 http://www.asp.net/web-forms/tutorials/security/roles/assigning-roles-to-users-cs

의 4 단계에서 가져온 createUserWiazrd 이름 RegisterUserWithRoles이다가

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

다섯 가지 역할이 있는데, 나는를 사용하여 확인했습니다.. 이 오류의 원인을 이해할 수 있도록 도와주십시오.

미리 감사드립니다. 나는이 기능을 위해 제공되는 마법사를 사용하지 않고, 스택 추적이 유용 할 수 있지만

+0

예외의 스택 추적은 무엇입니까? –

답변

0

, 나는 문제가 여기 -이다

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

의 FindControl의 ID는 SpecifyRolesStep을 선택하는 합리적으로 확신은 ASPX의 ID와 일치하지 않는 파일 -

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

심플 구문 (WizardStep) RegisterWithRoles.FindControl ("과 특이를 사용하여 주조 반대로 키워드 ("로 " yRolesStep "))는 FindControl이 객체를 반환하지 않거나 객체가 호환되지 않는 유형이기 때문에 SpecifyRolesStep이 null로 안전하게 설정된다는 것을 의미하므로 가능하면이"as "키워드를 사용하는 것이 좋습니다. 여기)와 다음이 항상 가능한 것은 아닙니다 물론 syntax-

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

다음 개체가 null의 프로그램의 흐름을 계속 사용하여 결과 객체를 조작하기 전에 널 (null)에 대한 검사, 그것은 중요 할 수있다 따라서 디버깅을 돕기 위해보다 구체적인 오류 메시지가있는 ApplicationException을 throw하는 것보다 오류를 처리하지 못할 수도 있습니다.

이 경우 개발자가 분명히 널 (null)을 검사하고 (코드를 어지럽 힙니다) 개발자가 판단 할 때 분명히 예외를 던지면 모두 판단해야합니다.

배경을 제외하고는 SpecifyRolesStep에 사용하는 FindControl 매개 변수를 변경하거나 ASPX의 WizardStep "SpecifyRoles"의 이름을 일치시켜야합니다.

관련 문제