Solved

NullReferenceException Unhandled when trying to use ConfigurationManager

Posted on 2008-10-01
3
868 Views
Last Modified: 2012-05-05
I'm trying to write a windows application that can retrieve a persistant setting I want to store in a setting file.  I believe I want to use configurationmanager.  When I run it I'm receiving the following

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="retrieveProperties"
  StackTrace:
       at retrieveProperties.DALText.GetProperties() in C:\TroyTemp\retrieveProperties\retrieveProperties\DALText.vb:line 20
       at retrieveProperties.Module1.Main() in C:\TroyTemp\retrieveProperties\retrieveProperties\Module1.vb:line 7
       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()


This is my first time and I'm likely doing something very simple wrong.
Imports System.Configuration
Public Class DALText
    Public Function GetProperties() As ArrayList
        Dim sSourceFile As String
        sSourceFile = System.Configuration.ConfigurationManager.AppSettings("SourceFile").ToString
 
    End Function
End Class
 
     ''Here is the contents of my app.config file
 
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="retrieveProperties.PropertiesSettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.diagnostics>
        <sources>
            <!-- This section defines the logging configuration for My.Application.Log -->
            <source name="DefaultSource" switchName="DefaultSwitch">
                <listeners>
                    <add name="FileLog"/>
                    <!-- Uncomment the below section to write to the Application Event Log -->
                    <!--<add name="EventLog"/>-->
                </listeners>
            </source>
        </sources>
        <switches>
            <add name="DefaultSwitch" value="Information" />
        </switches>
        <sharedListeners>
            <add name="FileLog"
                 type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"
                 initializeData="FileLogWriter"/>
            <!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
            <!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
        </sharedListeners>
    </system.diagnostics>
    <userSettings>
        <retrieveProperties.PropertiesSettings>
            <setting name="SourceFile" serializeAs="String">
                <value>"C:\TroyTemp\RawProperties.txt"</value>
            </setting>
        </retrieveProperties.PropertiesSettings>
    </userSettings>
</configuration>

Open in new window

0
Comment
Question by:bminetwork2277
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22621628
check this out:
 

--XML file
<userSettings>
    <WindowsApplication3.My.MySettings>
      
      <setting name="SourceFile" serializeAs="String">
        <value>"C:\TroyTemp\RawProperties.txt"</value>
      </setting>
    </WindowsApplication3.My.MySettings>
  </userSettings>
 
-- vb.net code
 
 Dim sett As My.MySettings
        sett = WindowsApplication3.My.Settings
        Dim sSourceFile As String
        sSourceFile = sett("SourceFile")

Open in new window

0
 
LVL 1

Author Comment

by:bminetwork2277
ID: 22647193
I am now getting
System.Configuration.SettingsPropertyNotFoundException was unhandled
  Message="The settings property 'SourceFile' was not found."
My Code is as follows

        Dim sett As My.MySettings
        sett = retrieveProperties.My.Settings
        Dim sSourceFile As String
        sSourceFile = sett("SourceFile")

My XML is as follows

      <userSettings>
            <retrieveProperties.My.MySettings>
                  <setting name="SourceFile" serializeAs="String">
                        <value>"C:\TroyTemp\RawProperties.txt"</value>
                  </setting>
            </retrieveProperties.My.MySettings>
      </userSettings>
0
 
LVL 13

Accepted Solution

by:
SameerJagdale earned 500 total points
ID: 22648802
have you tried my code..it works for me.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question