Got no clue why I am getting this error for treeView in a Webpart, could be a logical error to be honest,
tree.Nodes.Add(groupNode);
why would it say that :S
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
int Index = 0;
TreeView tree = new TreeView();
TreeNode groupNode;
Dictionary<int, string> GroupList = new Dictionary<int, string>();
Dictionary<int, string> UserList = new Dictionary<int, string>();
List<string> IndividualUserList = new List<string>();
foreach (SPUser user in SPContext.Current.Web.Users)
{
string groupName = FormatUserLogin(user.Name);
if (groupName != "" && groupName != "System Account")
IndividualUserList.Add(groupName);
else if (user.IsDomainGroup && !string.IsNullOrEmpty(groupName) &&
Directory.DoesGroupExist(groupName))
{
Index++;
GroupList.Add(Index, groupName);
List<ADUser> adUsers = Directory.GetUsersFromGroup(groupName);
foreach (ADUser member in adUsers)
{
if (member != null && !string.IsNullOrEmpty(member.DisplayName))
UserList.Add(Index, member.DisplayName);
}
}
}
IndividualUserList.Sort();
foreach (string Item in IndividualUserList)
{
groupNode = new TreeNode(Item);
}
foreach (KeyValuePair<int, string> GroupPair in GroupList)
{
groupNode = new TreeNode(GroupPair.Value);
foreach (KeyValuePair<int, string> UserPair in UserList)
{
if (UserPair.Key == GroupPair.Key)
{
groupNode.ChildNodes.Add(new TreeNode(UserPair.Value));
}
}
}
tree.Nodes.Add(groupNode);
this.Controls.Add(tree);
}
catch (Exception)
{
//loggingit
}
}
Cheers
Because you didn’t explicitly initilize that variable before use of it:
considering this suspicios code:
It’s not clear why you need to initialize the same instance all over the iteration, but, by the way, none gurantees that
IndividualUserListis not empty, so the variable can remain not initialized.To resolve this problem, at the begining of the function, write
or
EDIT
or, as Vlad suggested, coud choose:
The choice is based on your code-flow logic.