Start New Discussion within our Software Development Community

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?

This article has been dead for over six months. Start a new discussion instead.