C#:为什么我在代码中不断收到System.NullReferenceException

时间:2015-12-03 19:12:45

标签: c# xml winforms

我是Winforms和C#的新手。我的表单是读取XML并将其一些节点的值添加到表单textBoxes。此外,它应该通过用户插入文本框中的值更新/修改这些节点值,然后保存它们。这就是表格的样子:
Form Design

表单代码:
记住button4_Click ='添加按钮'。 button2_Click ='修改按钮'。

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace WindowsForm
{
public partial class Form1 : Form, INotifyPropertyChanged
{
    XmlDocument doc = new XmlDocument();
    XmlElement m_textElem1;
    XmlElement m_textElem2;
    XmlElement m_textElem3;

    public string TextElement1Content
    {
        get { return m_textElem1.InnerText; }
        set { m_textElem1.InnerText = value; }
    }

    public string TextElement2Content
    {
        get { return m_textElem2.InnerText; }
        set { m_textElem2.InnerText = value; }
    }

    public string TextElement3Content
    {
        get { return m_textElem3.InnerText; }
        set { m_textElem3.InnerText = value; }
    }
    public Form1()
    {
        InitializeComponent();
    }


    private void Form1_Load(object sender, EventArgs e)
    {

    }


    private void button2_Click(object sender, EventArgs e)

    {
        doc.Load("C:\\Users\\Fahad\\Documents\\Visual studio 2015\\Projects\\ClassLibrary1\\XMLFile3.xml");
        m_textElem1 = doc.SelectSingleNode("Twittercards/Card1/title") as XmlElement;
        m_textElem2 = doc.SelectSingleNode("Twittercards/Card1/image") as XmlElement;
        m_textElem3 = doc.SelectSingleNode("Twittercards/Card1/description") as XmlElement;

        textBox1.DataBindings.Add("Text", this, "TextElement1Content");
        textBox2.DataBindings.Add("Text", this, "TextElement2Content");
        richTextBox1.DataBindings.Add("Text", this, "TextElement3Content");

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button3_Click(object sender, EventArgs e)
    {

    }

    XmlDocument xDoc = null;

    private void button4_Click(object sender, EventArgs e)
    {
          OpenFileDialog ofd = new OpenFileDialog();
          if (ofd.ShowDialog() == DialogResult.OK)
           {

            XmlElement root = xDoc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("Twittercards/Card1");
            xDoc.Load(ofd.FileName);

            foreach (XmlNode node in nodes)
            {
                var node1 = xDoc.SelectSingleNode(@"Twittercards/Card1/title");
                node1.InnerText = textBox1.Text;
                var node2 = xDoc.SelectSingleNode(@"Twittercards/Card1/image");
                node1.InnerText = textBox2.Text;
                var node3 = xDoc.SelectSingleNode(@"Twittercards/Card1/description");
                node1.InnerText = richTextBox1.Text;
                xDoc.Save(ofd.FileName);
                // textBox1.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/title").InnerText;
                //  textBox2.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/image").InnerText;
                //  richTextBox1.Text = xDoc.SelectSingleNode(@"Twittercards/Card1/description").InnerText;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        doc.Save("C:\\Users\\Fahad\\Documents\\Visual studio 2015\\Projects\\ClassLibrary1\\XMLFile3.xml");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void NotifyPropertyChanged(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {

    }
}

}

“添加按钮”的例外来自:XmlElement root = xDoc.DocumentElement;

“修改按钮”的例外来自:

public string TextElement1Content
{
    get { return m_textElem1.InnerText; }
    set { m_textElem1.InnerText = value; }
}

如何修复此代码以解决我的原始任务?请帮忙。感谢

2 个答案:

答案 0 :(得分:0)

您必须在使用之前初始化对象,具体取决于您何时需要使用它。要么添加某种类似的逻辑:

if (m_textElem1==null) {
    // initialize the object
}

或者在构造函数中:

public Form1() {
    Initialize();
   // initialize the object here
}

答案 1 :(得分:0)

xDoc在button4_Click方法之前的行中声明为null。您无法访问null对象的属性。

我认为您需要在访问xDoc.DocumentElement属性之前移动xDoc.Load(ofd.FileName)行。

xDoc.Load(ofd.FileName);    
XmlElement root = xDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("Twittercards/Card1");

另外,您确定xDoc变量的范围吗?也许它需要被引入你的方法。