10

I have a class called LocalConstants....

 public static class LocalConstants {
    public static string DM_PATH = ConfigurationManager.AppSettings["DMQueue"];
    public static string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"];
 }

When trying to access this class in my main program I am getting a null reference exception. Anything from ConfigurationManager.AppSettings[ is always null. But if I write

  //The value is returned fine 
  string bo=ConfigurationManager.AppSettings["MSQueue"];

this compiles fine but is always null and throws a NullRefexception

   string moomoo = LocalConstants.PROJECT_PATH;

The exception is The type initializer for 'TestCodeOutOnSide.LocalConstants' threw an exception.

The innerException is the basic Object reference not set to an instance of an object.

Even if I change the PROJECT_PATH to

public static readonly string PROJECT_PATH = @"FORMATNAME:DIRECT=OS:serus-nickl\RMQDEV";

I get the same exception

Any ideas?

Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152

4 Answers4

5

To begin with, if you are doing this to provide some sort of performance benefit then you should know that these are cached. See ConfigurationManager.AppSettings Caching, to remove any.

Second the issue is most likely that Static field initialization does not work how you expect it to. So your code as written provides no guarantee of that ConfigurationManager.AppSettings has been run. From the linked article sample code:

might produce either the output:

Init A
Init B
1 1

or the output:

Init B
Init A
1 1

[EDIT per OP comment]

There must be something else involved as:

public static class LocalConstants
{
    public static string DM_PATH = "DMQueue";
    public static string PROJECT_PATH = "MSQueue";
}


class Program
{
    static void Main(string[] args)
    {
        string moomoo = LocalConstants.PROJECT_PATH;

        Console.WriteLine(moomoo);
    }
}

works for me.

[Edit 2 - Fro those who come after]

It looks like The type initializer for ‘SomeClass’ threw an exception can be a case where

But when it's called by the WPF designer, the "application" is Visual Studio, which (presumably) doesn't have the appropriate connection strings in its .config file;

The fix for that author was:

moving the instantiation of my Entity Data Model into a property

Community
  • 1
  • 1
Joshua Drake
  • 2,704
  • 3
  • 35
  • 54
  • Any suggestions on a better way to approach this? – Nick LaMarca May 15 '12 at 19:29
  • @NickLaMarca Why do you not like `string moomoo = ConfigurationManager.AppSettings["MSQueue"];`? What are you trying to accomplish with your `LocalConstants` class? – Joshua Drake May 15 '12 at 20:00
  • Requirement is no magic strings or ConfigurationManager references in code. Wanted all in 1 file. I just took all the strings in the ConfigurationManager and directly put them in the LocalConstants class and it still throwing an exception and returning null – Nick LaMarca May 15 '12 at 20:08
  • The type initializer for 'TestCodeOutOnSide.LocalConstants' threw an exception. – Nick LaMarca May 15 '12 at 20:10
  • @NickLaMarca Those strings are not _Magic_, they name things you are using, like a CONNECTION_STRING, and what have you. How is having your own file any different from using the existing `ConfigurationManager`? And what does "strings in the ConfigurationManager **and directly put them in the LocalConstants**" mean? – Joshua Drake May 15 '12 at 20:12
  • public static readonly string PROJECT_PATH = @"FORMATNAME:DIRECT=OS:serus-nickl\RMQDEV"; – Nick LaMarca May 15 '12 at 20:14
  • this is what is in LocalConstants now but same result it throws an exception – Nick LaMarca May 15 '12 at 20:15
  • @NickLaMarca So `string moomoo = LocalConstants.PROJECT_PATH;` throws that exception, and the only change to the LocalConstants class is that it now includes the line you show in your comment and not the one show in your question, correct? – Joshua Drake May 15 '12 at 20:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11279/discussion-between-joshua-drake-and-nick-lamarca) – Joshua Drake May 15 '12 at 20:24
1

Why don't try something like:

   public static string ProjectPath 
   { 
       get 
       { 
           return ConfigurationManager.AppSettings["MSQueue"]; 
       } 
   }
markov
  • 11
  • 1
  • 1
0

I called this

public static string Environment = AppEnvironmentVariable.ToUpper() != "PROD" ? "***FROM " + AppEnvironmentVariable.ToUpper() + "** " : "";

Before this

public static string AppEnvironmentVariable = "DEV";

In the LocalConstants file which broke it because of what Josh said about Static field initialization

Nick LaMarca
  • 8,076
  • 31
  • 93
  • 152
-1

You could try making them readonly

public static readonly string PROJECT_PATH = ConfigurationManager.AppSettings["MSQueue"];

readonly fields can be lazy-loaded

Mark
  • 1,455
  • 3
  • 28
  • 51