C# XML运行时错误/对象引用未设置为对象的实例。(使用C)

C# XML运行时错误/对象引用未设置为对象的实例。(使用C),c#,xml,runtime-error,object-reference,C#,Xml,Runtime Error,Object Reference,我正在寻找一个明智的向导来为我指明正确的方向。我正在使用XML和C制作一个模拟漫画数据库。除了我的编辑页面之外,其他一切都非常出色。在我的本地主机上,我得到一个对象引用错误,在我的主机上,我得到一个运行时错误 现场页面在这里:登录用户名是管理员,密码是adminpass! 我的代码如下: ComicEdit.aspx <%@ Page Title="" Language="C#" MasterPageFile="~/Admin/adminMaster.master" AutoEventW

我正在寻找一个明智的向导来为我指明正确的方向。我正在使用XML和C制作一个模拟漫画数据库。除了我的编辑页面之外,其他一切都非常出色。在我的本地主机上,我得到一个对象引用错误,在我的主机上,我得到一个运行时错误

现场页面在这里:登录用户名是管理员,密码是adminpass!

我的代码如下: ComicEdit.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/adminMaster.master" AutoEventWireup="true" CodeFile="comicsEdit.aspx.cs" Inherits="_Default" %>

<asp:Content ID="Content2" ContentPlaceHolderID="center" Runat="Server">
<asp:Label ID="lblOutput" runat="server"  Visible="true" />
<asp:Panel ID="pnlEdit" runat="server" Visible="false">
    <h1>
        Edit Comic Database
    </h1>

    <div id="divMessage">
    <asp:Label ID="lblMessage" runat="server"></asp:Label></div>
    <table>
        <tr>
            <td >
                <asp:Label ID="lblTitle" runat="server" Text="Label">Title:</asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtTitle" runat="server" Width="200px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="val1" runat="server" ErrorMessage="Please enter a title"
                    Display="Dynamic" ControlToValidate="txtTitle"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td >
                <asp:Label ID="lblIssue" runat="server" Text="Label">Issue:</asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtIssue" runat="server" Width="200px"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Please enter a Issue"
                    Display="Dynamic" ControlToValidate="txtIssue"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td >
                <asp:Label ID="lblDesc" runat="server" Text="Label">Description:</asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txtDesc" runat="server"  Width="200px" Height="200px" TextMode="MultiLine"></asp:TextBox>
                <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="Please enter a Description"
                    Display="Dynamic" ControlToValidate="txtDesc"></asp:RequiredFieldValidator>
            </td>
        </tr>
        <tr>
            <td  colspan="2">
                <asp:Button ID="btnEdit" OnClick="btnEdit_Click" runat="server" Text="Update Comic">
                </asp:Button>
            </td>
        </tr>
    </table>
</asp:Panel>
</asp:Content>
我实在是受够了,我从零开始写这个文件已经6次了。有没有人愿意为这个令人悲伤的问题透露一些信息? 我将非常非常感激。在过去的48个小时里,我一直在3小时的睡眠中工作,所以我真的会亲吻你的脚

最好的! 劳拉

问题可能是在您调用btnEdit\u单击时未加载myXmlDocument

在您的btnEdit\u单击方法中

public void btnEdit_Click(object sender, EventArgs e)
{
    if(myXmlDocument != null && GrabComic != null && GrabComic.ChildNodes.Count >3){

        GrabComic.ChildNodes[0].InnerText = txtTitle.Text; 
        GrabComic.ChildNodes[1].InnerText = txtIssue.Text;
        GrabComic.ChildNodes[2].InnerText = txtDesc.Text;
        myXmlDocument.Save(Path.Combine(Request.PhysicalApplicationPath, "comic.xml"));

        lblMessage.Text = "You have successfully updated the Database";
    }
}

为什么在堆栈跟踪上看不到??我猜您的一个子节点[0]或所有子节点都为空。或者在方法上设置一个断点,确保对象不为null。

为什么不试着调试哪行代码会出现异常?这就是问题所在。非常感谢您的详细解释。当我有机会看到你的代码和我的代码时,我发现我的眼睛呆呆地看着错误是什么,我能看到错误在哪里,我感谢你抽出时间来帮助我。你是摇滚明星。多谢各位
string file = Path.Combine(Request.PhysicalApplicationPath, "comic.xml");
if(File.Exists(file))
{
    myXmlDocument.Load(file);
    XmlNode rootNode = myXmlDocument.DocumentElement;
    if(rootNode !=null && rootNode.ChildNodes.Count> intComicID)
    {
       GrabComic = rootNode.ChildNodes[intComicID - 1];
            if (GrabComic == null)
            {// invalid id
                lblOutput.Visible = true;
                lblOutput.Text = "item doesn't exist.";
            }
            else
            {// valid id
                pnlEdit.Visible = true;
                txtTitle.Text = GrabComic.ChildNodes[0].InnerText;
                txtIssue.Text = GrabComic.ChildNodes[1].InnerText;
                txtDesc.Text = GrabComic.ChildNodes[2].InnerText;
            }
    }

}
public void btnEdit_Click(object sender, EventArgs e)
{
    if(myXmlDocument != null && GrabComic != null && GrabComic.ChildNodes.Count >3){

        GrabComic.ChildNodes[0].InnerText = txtTitle.Text; 
        GrabComic.ChildNodes[1].InnerText = txtIssue.Text;
        GrabComic.ChildNodes[2].InnerText = txtDesc.Text;
        myXmlDocument.Save(Path.Combine(Request.PhysicalApplicationPath, "comic.xml"));

        lblMessage.Text = "You have successfully updated the Database";
    }
}