Hello!

private void btnRecord_Click(object sender, EventArgs e)
        {
            //try
            //{
                Random num = new Random(2);
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement contact = xmlDoc.CreateElement("contact");
                XmlElement name = xmlDoc.CreateElement("name");
                XmlElement familyname = xmlDoc.CreateElement("familyname");
                XmlElement momos = xmlDoc.CreateElement("momos");

                XmlAttribute contactid = xmlDoc.CreateAttribute("id");
                contactid.Value = num.ToString();

                XmlText firtnametext = xmlDoc.CreateTextNode(tbName.Text);
                XmlText lastname = xmlDoc.CreateTextNode(tbFamilyName.Text);
                XmlCDataSection datasec = xmlDoc.CreateCDataSection(tbMomos.Text);

                contact.Attributes.Append(contactid);
                contact.AppendChild(name);
                contact.AppendChild(familyname);
                contact.AppendChild(momos);

                name.AppendChild(firtnametext);
                familyname.AppendChild(lastname);
                momos.AppendChild(datasec);

                xmlDoc.DocumentElement.AppendChild(contact);
                xmlDoc.Save(Application.StartupPath + "/contactsList.xml");

                tbFamilyName.Text = "";
                tbMomos.Text = "";
                tbName.Text = "";
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("" + ex.Message);
            //}

        }

Error: Object reference not set to an instance of an object.
What should I do. Where to use 'new' operator?

Dani AI

Generated

The NullReferenceException comes from calling xmlDoc.DocumentElement.AppendChild(contact) when xmlDoc has no root element. A fresh new XmlDocument() has an empty document (no document element), so DocumentElement is null. See the .NET docs for XmlDocument.DocumentElement for details: XmlDocument.DocumentElement.

Two safe fixes: load the existing XML file first (if it exists), or create and append a root element before adding contact. Example pattern:

var path = Path.Combine(Application.StartupPath, "contactsList.xml");
if (File.Exists(path))
{
    xmlDoc.Load(path);
}
else
{
    var root = xmlDoc.CreateElement("contacts");
    xmlDoc.AppendChild(root);
}

xmlDoc.DocumentElement.AppendChild(contact);
xmlDoc.Save(path);

Other practical notes tied to the thread: was right to check namespaces — ensure using System.Xml; is present. Also fix how the id is generated: new Random(2) always uses the same seed and num.ToString() returns the Random object string, not a numeric id. Use Guid.NewGuid().ToString() or new Random() with Next() for unique IDs. Wrap file operations in try/catch, validate Application.StartupPath/file permissions, and consider adding an XML declaration (xmlDoc.CreateXmlDeclaration) if consumers expect it. These small checks will eliminate the null reference and produce a well-formed, appendable XML document.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.