0

I have a public static class that has a number of static methods and static readonly fields.

public static class Global
{
    ...
    // Config is a static method in Global.
    public static readonly string FirebaseAuthDomain = Config("FirebaseAuthDomain"); 
    private static readonly string ResourceNameFormat = "{0}.{1}";
    ...
}

I get a NullReferenceException when I access ResourceNameFormat. However, if I move the declaration of ResourceNameFormat before FirebaseAuthDomain as shown below, it works fine.

public static class Global
{
    ...
    private static readonly string ResourceNameFormat = "{0}.{1}";
    // Config is a static method in Global.
    public static readonly string FirebaseAuthDomain = Config("FirebaseAuthDomain"); 
    ...
}

It looks like the call to Config() has something to do with this behavior, but I can't tell why. What is the difference between these two code snippets?

nlogn
  • 343
  • 4
  • 13
  • 2
    Could be that your `Config()`is using `ResourceNameFormat ` in some way ? – Tigran Sep 07 '17 at 21:56
  • `I get a NullReferenceException when I access ResourceNameFormat.` Can you show us the calling code which experiences the NRE? – mjwills Sep 07 '17 at 21:58
  • @Tigran Yes, I have just noticed that there are a couple of nested method calls where calling `Config()` results in reading `ResourceNameFormat`. That explains it. – nlogn Sep 07 '17 at 22:00
  • @mjwills Thanks for the link, I didn't see that post before. – nlogn Sep 07 '17 at 22:02

0 Answers0