简体   繁体   中英

C# & XML - Object reference not set to an instance of an object

Good Evening StackOverFlowers,
I have recently starting coding in XML via Visual Studio 2010. I have come across what seems to be a simple solution, yet the solution escapes me. I get an error for an Object Reference not set, but I don't see what I haven't set. (error here: http://i.imgur.com/CVaxY.png )

My codebehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
  {
    int intDVDID;
    XmlDocument myXmlDocument = new XmlDocument();
    XmlNode rootNode;
    XmlNode selectedDVD;

public void Page_Load(object Src, EventArgs E)
{
    intDVDID = Convert.ToInt32(Request.QueryString["id"]);

    myXmlDocument.Load(Request.PhysicalApplicationPath + @"dvd.xml");
    rootNode = myXmlDocument.DocumentElement;
    selectedDVD = rootNode.ChildNodes[intDVDID - 1];
    if (!Page.IsPostBack)
    {
        rootNode.RemoveChild(selectedDVD);
        myXmlDocument.Save(Request.PhysicalApplicationPath + @"dvd.xml");
        lblMessage.Text = "You have successfully deleted the DVD";
    }
  }
}

Is it just a matter of saying:

  int intDVDID = new intDVDID 

I know by reading this you're all going to want to pull your hair out at my inexperience and lack of understanding how to solve this, but I appreciate your time and your patience just looking.

Best regards, Laura :)

Edit: Here is my XML:

<?xml version="1.0" encoding="utf-8" ?>
<!-- This XML document describes a DVD library -->
<library>
  <DVD id="1">
    <title>Breakfast at Tiffany's</title>
    <format>Movie</format>
    <genre>Classic</genre>
  </DVD>
  <DVD id="2">
    <title>Contact</title>
    <format>Movie</format>
    <genre>Science fiction</genre>
  </DVD>
  <DVD id="3">
    <title>Little Britain</title>
    <format>TV Series</format>
    <genre>Comedy</genre>
  </DVD>
</library>

It is looking like your selectedDVD may null, put some null checks to verify.

if(selectedDVD != null)
{
}

Edit: In response to your question in comments. Here is some example code. I threw in an xpath even though it seems your case is very simple you may want to use this in the future

string xml = "<xml><node id='1'></node><node id='2'></node></xml>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);           

//This is an xpath(This replaces your .DocumentElement.ChildNodes[index]
XmlNode desiredNode = xmlDoc.DocumentElement.SelectSingleNode("node[@id='1']");
if (desiredNode != null)
{
    xmlDoc.DocumentElement.RemoveChild(desiredNode);
}//if             

Pleaase make sure that your query string is not empty it may cause you null reference exeption that you are geting.

if(Request.QueryString["id"]!="")
{
    intDVDID = Convert.ToInt32(Request.QueryString["id"]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM