0

Hi I'm trying to disable some users in my active directory with the following code below but I keep getting an "Object reference not set to an instance of an object" error message as shown in the attached image below.

I understand that this problem is because some of my users might be null.

What is the best way for me to go about resolving this?

DisableADUser method

//disable invalid accounts
    private static bool DisableADUser(string samAccountName)
    {
        bool result = false;
        try
        {    
            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
                    (principalContext, samAccountName);
            userPrincipal.Enabled = false;
            userPrincipal.Save();
            if (userPrincipal.Enabled == false)
            {
                Console.WriteLine("Account has been disabled successfully");
                result = true;
            }
            else
            {
                Console.WriteLine("\nUnable to disable account");
                result = false;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return result;
    }

Object reference not set to an instance of an object Error

Thanks in advance :)

EDITED CODE:

//disable invalid accounts
    private static bool DisableADUser(string samAccountName)
    {
        bool result = false;
        try
        {                
            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
            UserPrincipal userPrincipal = UserPrincipal.FindByIdentity
                    (principalContext, samAccountName);
            if (userPrincipal != null)
            {
                userPrincipal.Enabled = false;
                userPrincipal.Save();
                if (userPrincipal.Enabled == false)
                {
                    Console.WriteLine("Account has been disabled successfully");
                    result = true;
                }
                else
                {
                    Console.WriteLine("\nUnable to disable account");
                    result = false;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        return result;
    }
Community
  • 1
  • 1
coder123
  • 47
  • 2
  • 10
  • UserPrincipal.FindByIdentity() takes three parameters, but yours only have two. Are you sure it's correct? https://msdn.microsoft.com/en-us/library/bb344891%28v=vs.110%29.aspx – Steven Manuel Apr 07 '15 at 01:52
  • 1
    @StevenManuel, not exactly : https://msdn.microsoft.com/en-us/library/bb383475(v=vs.110).aspx – Saagar Elias Jacky Apr 07 '15 at 01:55
  • What has the step-through debugger suggested the issue is? – Ben Apr 07 '15 at 01:57
  • Are there existing users with those usernames? Nevertheless you should check to make sure userPrincipal is not null before setting enabled to false, this is standard practice to avoid null exception. – Steven Manuel Apr 07 '15 at 02:01
  • Hi all, thanks for your responses. I've managed to fix that issue by following the link suggested above. (edited code can be found above) I have a question though: How do I deal with the users with a null value? I still have to disable them somehow – coder123 Apr 07 '15 at 02:20

0 Answers0