Hi I am having trouble fixing this error: Object reference not set to an instance of an object. The error says that it occurs at the source: GetWebSiteData(), and I understand that it is saying that when it pulls the list of websites from my Settings.xml file it returns a null, so I am just trying to figure out why it returns a null eventhough the websites are located in my Settings.xml file. Any help would be greatly appreciated. Thanks

namespace SPF_User_Profile_Sync
{
    // -----------------------------------------------------------------------------
    // Provides a Global method for working with Configuration Settings for a Solution Based on an XML file
    public static class clsSettingsHandler
    {
        private const string m_CLASS_NAME = "clsSettingsHandler";

        private static string m_SettingsFile = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "\\Settings.xml";

        //Flags if we are running silent or not
        public static bool IsRunningSilent = false;

        //Just makes accessing settings easier from code
        public enum enuSettingChoice
        {
            ADServer,
            ADPort,
            ADUserName,
            ADUserPassword,
            SPSites
        }

        // -----------------------------------------------------------------------------
        // This translates the Enumerations to the String Paths that are used in the actual File
        private static string TranslateSettingFlag(enuSettingChoice pFlag)
        {
            string strReturn = "";

            switch (pFlag)
            {
                case enuSettingChoice.ADServer:
                    strReturn = "/Settings/AD/Server";
                    break;

                case enuSettingChoice.ADPort:
                    strReturn = "/Settings/AD/Port";
                    break;

                case enuSettingChoice.ADUserName:
                    strReturn = "/Settings/AD/UserName";
                    break;

                case enuSettingChoice.ADUserPassword:
                    strReturn = "/Settings/AD/UserPassword";
                    break;

                case enuSettingChoice.SPSites:
                    strReturn = "/Settings/SPSites";
                    break;
            }

            return strReturn;
        }

        // -----------------------------------------------------------------------------
        // Pulls a list of WebSites
        public static List<System.Xml.XmlNode> GetWebSiteData()
        {
            List<System.Xml.XmlNode> myReturnList = new List<System.Xml.XmlNode>();

            try
            {
                //Just as a safety net, we make sure that each has an actual URL
                foreach (System.Xml.XmlNode myNode in GetChildNodes(TranslateSettingFlag(enuSettingChoice.SPSites), true))
                {
                    if (myNode.Attributes.Count > 0 && myNode.Attributes[0].Name == "URL")
                    {
                        myReturnList.Add(myNode);
                    }
                }
            }
            catch (Exception ex)
            {
                clsErrorHandler.ShowError(m_CLASS_NAME, "GetWebSiteData", ex.Message, String.Empty);
            }
            finally
            {
            }

            return myReturnList;

        }

        // -----------------------------------------------------------------------------
        // Pulls a Single Value
        public static string GetSetting(enuSettingChoice pSetting)
        {
            string strValue = "";

            try
            {
                strValue = GetSingleValue(TranslateSettingFlag(pSetting));
            }
            catch (Exception ex)
            {
                clsErrorHandler.ShowError(m_CLASS_NAME, "GetSetting", ex.Message, String.Empty);
            }
            finally
            {
            }

            return strValue;
        }

        // -----------------------------------------------------------------------------
        // Reads a list of sub nodes from the file
        private static List<System.Xml.XmlNode> GetChildNodes(string FullParentNodePath, bool IncludeSubNodes)
        {
            string strTemp = "";
            List<System.Xml.XmlNode> listValues = new List<System.Xml.XmlNode>();

            System.Xml.XmlDocument xmlDoc = null;
            System.Xml.XmlNode xmlParentNode = null;

            try
            {
                // Make any needed adjustments to the Path passed
                FullParentNodePath = FullParentNodePath.Replace("\\", "/");
                if (FullParentNodePath.StartsWith("/") == true)
                {
                    FullParentNodePath = FullParentNodePath.Substring(1);
                }
                if (FullParentNodePath.EndsWith("/") == true)
                {
                    FullParentNodePath = FullParentNodePath.Remove(FullParentNodePath.Length - 1);
                }

                // Create the XML Document object that contains our info
                xmlDoc = new System.Xml.XmlDocument();
                xmlDoc.Load(m_SettingsFile);

                // Pull ALL the Child Nodes for the Tree Passed
                xmlParentNode = xmlDoc.SelectSingleNode(FullParentNodePath);
                foreach (System.Xml.XmlNode xmlNode in xmlParentNode)
                {
                    strTemp = xmlNode.Name;
                    if (strTemp.Length > 0)
                    {
                        listValues.Add(xmlNode.CloneNode(IncludeSubNodes));
                    }
                } //Next xmlNode
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                xmlParentNode = null;
                xmlDoc = null;
            }

            return listValues;
        }

        // -----------------------------------------------------------------------------
        // Reads a single value from a node. In the case of multiple values, it returns the last item
        private static string GetSingleValue(string FullNodePath)
        {
            List<string> listValues;

            try
            {
                listValues = GetValues(FullNodePath);
                if (listValues.Count > 0)
                {
                    return listValues[listValues.Count - 1];
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                listValues = null;
            }

            return "";
        }

        // -----------------------------------------------------------------------------
        // Reads all the values from a node
        private static List<string> GetValues(string FullNodePath)
        {
            string strTemp = "";
            List<string> listValues = new List<string>();

            System.Xml.XmlDocument xmlDoc;
            System.Xml.XmlNodeList xmlNodes;

            try
            {
                //Make any needed adjustments to the Path passed
                FullNodePath = FullNodePath.Replace("\\", "/");
                if (FullNodePath.StartsWith("/") == true)
                {
                    FullNodePath = FullNodePath.Substring(1);
                }
                if (FullNodePath.EndsWith("/") == true)
                {
                    FullNodePath = FullNodePath.Remove(FullNodePath.Length - 1);
                }

                // Create the XML Document object that contains our info
                xmlDoc = new System.Xml.XmlDocument();
                xmlDoc.Load(m_SettingsFile);

                // Pull ALL the Nodes for the Tree Passed
                xmlNodes = xmlDoc.SelectNodes(FullNodePath);
                foreach (System.Xml.XmlNode xmlNode in xmlNodes)
                {
                    strTemp = xmlNode.InnerText;
                    if (strTemp.Trim().Length > 0)
                    {
                        listValues.Add(strTemp);
                    }
                } //Next xmlNode
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                xmlNodes = null;
                xmlDoc = null;
            }

            return listValues;
        }
    } //end class

} //End Namespace

Are you sure there are always childnodes? You could test with this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.