0

I keep getting Null Reference Exception on this section of my code:

  enumerator = this.Ds_Settings1.Tables["Settings_RefreshForm_ScriptMgmt_SelectALL"].Rows.GetEnumerator();

Any assistance is greatly appreciated.

Here's the whole code. This is to create a tree view to list out all the scripts using a data set with stored proc.

    private void Refresh_Form()
    {
        IEnumerator enumerator = null;
        IEnumerator enumerator1 = null;
        IEnumerator enumerator2 = null;
        try
        {
            this.tv_Scripts.Nodes.Clear();
            this.tv_Scripts.Nodes.Add(Conversions.ToString(0), "Standard Fixes");
            this.tv_Scripts.Nodes.Add(Conversions.ToString(0), "SQL Agent Updates");
            this.tv_Scripts.Nodes.Add(Conversions.ToString(0), "Other");
            try
            {

                enumerator = this.Ds_Settings1.Tables["Settings_RefreshForm_ScriptMgmt_SelectALL"].Rows.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    DataRow script = (DataRow)enumerator.Current;
                    try
                    {
                        enumerator1 = this.tv_Scripts.Nodes.GetEnumerator();
                        while (enumerator1.MoveNext())
                        {
                            TreeNode ParentNode = (TreeNode)enumerator1.Current;
                            if (Microsoft.VisualBasic.CompilerServices.Operators.CompareString(ParentNode.Text, script[4].ToString(), false) != 0)
                            {
                                continue;
                            }
                            ParentNode.Nodes.Add(script[1].ToString(), script[1].ToString(), 1, 1);
                            int index = ParentNode.Index;
                            this.TreeNode_SetStateImageIndex(ParentNode, Conversions.ToInteger(index.ToString()));
                        }
                    }
                    finally
                    {
                        if (enumerator1 is IDisposable)
                        {
                            (enumerator1 as IDisposable).Dispose();
                        }
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
            this.cmb_Environment.Items.Clear();
            try
            {
                enumerator2 = this.Ds_Settings1.Tables["Settings_Environments_Select"].Rows.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    object env = RuntimeHelpers.GetObjectValue(enumerator2.Current);
                    object[] objArray = new object[] { 5 };
                    if (!Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(NewLateBinding.LateIndexGet(env, objArray, null), true, false))
                    {
                        continue;
                    }
                    ComboBox.ObjectCollection items = this.cmb_Environment.Items;
                    object[] objArray1 = new object[] { 1 };
                    items.Add(NewLateBinding.LateIndexGet(env, objArray1, null).ToString());
                }
            }
            finally
            {
                if (enumerator2 is IDisposable)
                {
                    (enumerator2 as IDisposable).Dispose();
                }
            }
            this.tv_Scripts.ExpandAll();
        }
        catch (Exception exception)
        {
            ProjectData.SetProjectError(exception);
            this.DisplayOnly_ErrorHandler("ERROR REFRESHING FORM DATA: ", exception.Message, MsgBoxStyle.Critical);
            ProjectData.ClearProjectError();
        }
    }

Here's the stack trace:

at SQLRefreshTool.SQLRefreshTool.Refresh_Form() in C:\Users\BSantiago\Documents\Visual Studio 2010\Projects\SQLRefreshTool\SQLRefreshTool\Form1.cs:line 179 at SQLRefreshTool.SQLRefreshTool..ctor() in C:\Users\BSantiago\Documents\Visual Studio 2010\Projects\SQLRefreshTool\SQLRefreshTool\Form1.cs:line 123 at SQLRefreshTool.Program.Main() in C:\Users\BSantiago\documents\visual studio 2010\Projects\SQLRefreshTool\SQLRefreshTool\Program.cs:line 18 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

Ankush Madankar
  • 3,364
  • 4
  • 34
  • 69

1 Answers1

0

NullReferenceException tells you that you are trying to access an attribute or method on a null object. Although type compatible (no compile time error), as the attr/method is defined for the type of the object, at runtime your object turns out to be null.

There are two possible causes:

either

this.Ds_Settings1

or

this.Ds_Settings1.Tables["Settings_Environments_Select"]

is null.

Check each of them in debug mode.

It is improbable that the exception is caused by Rows, as that should be an empty list, and not null, even if your table has no rows.

nestedloop
  • 2,486
  • 21
  • 33