1

I am .net beginner. I have gone through many sites before asking here. I am getting error -- "Object reference not set to an instance of an object." .This error comes usually when there are null values in any control but in my case Every control has some text in them, then why this error coming? here is my xml file

cmbProduct        --> combobox 
txtNewBrand       --> textBox
txtUpdateQuantity --> textBox
txtUpdatePrice    --> textBox

I tried the below code:

onButtonClick

XElement doc = XElement.Load(@"..\..\stock.xml");
var newElement = new XElement("items",
                               new XElement("productname", cmbProduct.Text),
                               new XElement("brandname", txtNewBrand.Text),
                               new XElement("quantity", txtUpdateQuantity.Text),
                               new XElement("price", txtUpdatePrice.Text));
 /*ERROR*/      doc.Element("stock").Add(newElement);
                doc.Save(xpath);
                MessageBox.Show("updated successfully");

EDIT :

Instead of using

XElement doc = XElement.Load(@"..\..\stock.xml");

i used

var doc = XDocument.Load(@"..\..\stock.xml");

and the problem solved. why so?

Mr_Green
  • 40,727
  • 45
  • 159
  • 271

4 Answers4

1

eather doc.Element("stock") can't be found and is NULL or doc is NULL

m4ngl3r
  • 552
  • 2
  • 17
1

Given the limited code it isn't easy to see what you have added and/or asserted to exist. Try adding these two lines above your error and the error message will indicate the fault.

Debug.Assert(doc != null, "Can not operate without a valid instance of 'doc'");
Debug.Assert(doc.Element("stock") != null, "Need the stock element to add to!");

You may need to include "using System.Diagnostics;" at the top of the file.

Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
  • it is showing these diagnostics.. which i cant understand http://i47.tinypic.com/25u1enn.jpg – Mr_Green Oct 09 '12 at 09:00
  • The first line is "need stock element to add to" - which is the 2nd line of the code I said to insert. Therefore, you need to add a "stock" to doc. e.g. `var stock = doc.Add(new Element("stock"));` and change your line to `stock.Add(newElement)` – Ray Hayes Oct 09 '12 at 09:05
  • well just for checkin i added `var doc = XDocument.Load(@"..\..\stock.xml");` this worked. why i cant understand. this code `XElement doc = XElement.Load(@"..\..\stock.xml");` became useless here? – Mr_Green Oct 09 '12 at 09:10
1

I guess you didn't pre-load the doc with an existing XML, if so there won't be any stock element to start with.

Try adding this

if (doc.Element("stock") == null)
{
    doc.Add(new XElement("stock"));
}

before

doc.Element("stock").Add(newElement);
xing
  • 447
  • 2
  • 6
1

You are getting the exception because:

doc.Element("stock").Add(newElement);

stock is the root node, and doc.Element("stock") returns null. What you are actually trying to do is to add an item in your xml. Try the following:

doc.Add(newElement);

This will give you the desired result.

Habib
  • 219,104
  • 29
  • 407
  • 436