0

I have a C# class with a field and a property that looks like this.

public static class Config {
    // ...
    private static string admin_email;
    public static string AdminEmail {
        get {
            if (admin_email == null) {
                admin_email = config_xml.Element("admin_email").Value;
            //  ^ The exception is thrown here.
            }
            return admin_email;
        }
    }
}

In the above code, config_xml is an XElement which contains a child element that looks like

<admin_email>[email protected]</admin_email>

However, when I try to access this property, I get a NullReferenceException even though the debugger shows that nothing is null.

I checked the debugger, and watching config_xml.Element("admin_email").Value shows the email, as expected.

The weird part is that when I put a breakpoint on that line and step in one step at a time there is no exception thrown.

I have tried with and without enabling the option Just My Code.

In case this helps, I try to access the property on a line like this (from a different project)

message.From = new MailAddress(Config.AdminEmail);

Edit

After changing the code to this, I realised that c was still null.

get {
    if (admin_email == null) {
        XElement c = config_xml;
        XElement e = c.Element("admin_email");
    //  ^ Exception is now thrown here
        string v = e.Value;
        admin_email = v;
    }
    return admin_email;
}
4
  • 4
    What's the stack trace for the exception? Maybe it's pointing somewhere else? If the exception doesn't happen during debugging then maybe there's a timing issue. Try outputting/logging somewhere the values of config_xml and config_xml.Element("admin_email") before referencing each of them. If the exception gets thrown from that line again, that log output should tell you what's null. Commented Jul 23, 2020 at 17:56
  • 1
    I'd wonder if the xml source is not loading as expected unless your in the debug build. If it's not throwing or logging when the xml source is read you won't notice till something like this occurs. Commented Jul 23, 2020 at 18:09
  • 1
    Store config_xml.Element("admin_email") into a variable of its own and add a breakpoint, my guess is that this will return null in your case. Commented Jul 23, 2020 at 18:14
  • Regarding your edit - something else is going on. A call to .Element(...) does not throw if no element is found. See: learn.microsoft.com/en-us/dotnet/api/… Commented Jul 23, 2020 at 18:27

1 Answer 1

1

Thank you David, asawyer, and Lasse V. Karlsen for helping me realise my mistake. I changed my code to this, and now it works.

admin_email = new Email(ConfigXml.Element("admin_email").Value;

I was using a similar technique for config_xml and ConfigXml, so I would only load the XML into the field config_xml if it was ever needed, and I forgot to access it with the property ConfigXml (which did the loading) instead of the field config_xml (which was null until I used the property).

I don't know why it was working with a breakpoint, maybe when I watched the property it assigned it? I don't know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.