0

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: https://i.sstatic.net/CDZgG.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>
4
  • What does dvd.xml look like? Commented Apr 17, 2012 at 4:41
  • Edited to add XML. Thanks for the ask Climbage! Commented Apr 17, 2012 at 4:46
  • What's the value of id when you get this error? Commented Apr 17, 2012 at 4:56
  • It was 1. I'm not sire what was going on. Commented Apr 17, 2012 at 5:11

2 Answers 2

1

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             
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this. I'm going to test this now. I appreciate your patience.
Okay, so I popped in some Null checks and I still get the same result. In the change of making myself look ridiculously more stupid than I already have... do to a null check, I just do something like this?: ` if (selectedDVD != null) { selectedDVD = rootNode.ChildNodes[intDVDID - 1]; } `
Thank you so much Brian. I truly appreciate the help. You're a star!
0

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"]);
}

1 Comment

Thank you so much Cold Told! You're help was fantastic. Helped me solve some issues. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.