Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi my config file look like this

XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        

        
        <sectionGroup name="connections" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" >
            <section name="test2" type="ELT.credentials, ELT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            <section name="test1" type="ELT.credentials, ELT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
        </sectionGroup>
    </configSections>
 
    
        <connections>
                <test2 connectionName="test2" dbUserID="test2"/>
                <test1 connectionName="test1" dbUserID="test1"/>

           </connections>
</configuration>


and i am trying to access the elements of section like this

C#
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"abcd.exe.config";
                Configuration config1 = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                string str = cmbConnections.SelectedItem.ToString();
                credentials credential1 = new credentials();
                credential1 = config1.GetSection(str) as credentials;
                txtDatabaseUserId.Text = credential1.DBUserID;


and my credential file is

C#
class credentials : ConfigurationSection
    {
        public credentials()
        {
        }
        [ConfigurationProperty("connectionName")]
        public string ConnectionName
        {
            get { return (string)this["connectionName"]; }
            set { this["connectionName"] = value; }
        }
        [ConfigurationProperty("dbUserID")]
        public string DBUserID
        {
            get { return (string)this["dbUserID"]; }
            set { this["dbUserID"] = value; }
        }
     }


When i run this code it is giving me an error "object reference not set to an instance of variable". How can i get the values for dbuserID for all the sections.
Posted

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.


For now, since you have not shared the exact line, it would be little difficult to point you out in your code shared as I see good number of places where this can occur.
 
Share this answer
 
Comments
nishanjain 6-Sep-12 0:13am    
credential1 = config1.GetSection(str) as credentials;
The error is in this line
Sandeep Mewara 6-Sep-12 0:56am    
This means either config1 is NULL or config1.GetSection(str) returned NULL.

Check and resolve.
nishanjain 6-Sep-12 1:00am    
config1.GetSection(str) returned NULL.
Espen Harlinn 6-Sep-12 10:14am    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900