Null pointer errors with Static methods
<p>I have getting a number of error reports from an application of mine that don't seem to make any sense. I have a static class that has a number of public static methods that are called from other parts of my code. Most of these methods create an element on an XML document.</p>

<p>The first line in most of these methods is a call to a private static method in this same class which creates the element on the XML doc and returns it, like so:</p>

<pre><code>private static XmlElement BuildCmd(string name)
{
  XmlElement e = OtherClass.NewElement("CMD");
  e.SetAttribute("type", name);
  return e;
}

public static uint SendFlightReport(FlightData info, AircraftInfo aInfo)
{
  XmlElement cmd = BuildCmd("pirep");
  ....
  some other stuff
  ....
}
</code></pre>

<p>The stack dumps generated by the exception all report the line calling BuildCmd as the offending line, which makes no sense - how can you have a null pointer (or, in C# - Object reference not set to an instance of an object) when calling a static method?</p>

<p>EDIT: For those who asked, here is the code to OtherClass.NewElement:</p>

<pre><code>public class OtherClass {

   private readonly XmlDocument doc = new XmlDocument();

   public XmlElement NewElement(string name) {
      lock (doc)
        return doc.CreateElement(name);
   }
}
</code></pre>
