error as object reference not set to an instance of an object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maddy1
    New Member
    • Feb 2007
    • 2

    error as object reference not set to an instance of an object

    HELLO

    when ever im trying to execute this, im getting an error as object reference not set to an instance of an object

    private void button2_Click(o bject sender, EventArgs e)
    {

    try
    {
    button1.Enabled = false;
    XmlDocument doc = new XmlDocument();
    doc.Load("C:\\D ocuments and Settings\\srakk li\\Desktop\\sa mple.xml");
    XmlNodeList xnl = doc.SelectNodes ("//info");
    foreach (XmlNode node in xnl)
    {

    if (node.Attribute s["salary"].Value == null)
    {
    Console.WriteLi ne("no col");
    }
    else
    {
    node.Attributes["salary"].Value = (Int32.Parse(no de.Attributes["salary"].Value) + 100.ToString()) ;
    }
    }

    doc.Save("C:\\D ocuments and Settings\\srakk li\\Desktop\\sa mple.xml");


    }
    catch (SqlException ee)
    {
    Console.WriteLi ne(ee);
    Console.ReadLin e();
    }
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Which object and line is the error referring to?

    Comment

    • maddy1
      New Member
      • Feb 2007
      • 2

      #3
      Originally posted by kenobewan
      Which object and line is the error referring to?
      Hello

      i get that error in the following line:
      node.Attributes["salary"].Value = (Int32.Parse(no de.Attributes["salary"].Value) + 100.ToString()) ;

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Originally posted by maddy1
        Hello

        i get that error in the following line:
        node.Attributes["salary"].Value = (Int32.Parse(no de.Attributes["salary"].Value) + 100.ToString()) ;
        What does 100.ToString() do? is 100 a primitive data type? How can you do a dot operation on a primitive?

        Is this some kind of goofy VB.net thing?

        Comment

        • enreil
          New Member
          • Jan 2007
          • 86

          #5
          I think your parentheses are goofy, and as a result it looks like you are parsing something that is already an int32, trying to add a string "100" to it, and then placing it back into an int32. The following should accomplish the same purpose more efficiently:

          node.Attributes["salary"].Value += 100;

          Originally posted by maddy1
          Hello

          i get that error in the following line:
          node.Attributes["salary"].Value = (Int32.Parse(no de.Attributes["salary"].Value) + 100.ToString()) ;

          Comment

          Working...