Click here to Skip to main content
15,944,397 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to add a new node to a xml file. The code that I wrote works fine when there is a node exists but if not it throws an error of "object referance not set to an instance of an object." Basiccaly code copies a node and then writes data to the child nodes of the parent node. When I debug this code;

XmlNode credentialsnodedetails = xmlDocument.SelectNodes("/Credentials/Credential")[0];
            XmlNode newnode = credentialsnodedetails.CloneNode(true);

I see that "credentialsnodedetails" value is  NULL. I think because it is null I am having the problem. What should do programatically to avoid it?
Dr.Walt Fair, PE 23-Nov-10 18:06pm    
Is that node supposed to be NULL? Without knowing the details, it looks like you need to check for that first.
Orcun Iyigun 23-Nov-10 20:14pm    
Yeah I thought of it. Earlier I tried to check if it is NULL or not. If it is null I need to create a new node but I dont know how to do it. thats what I tried to ask seems like I wasnt clear enough.

1 solution


Have you look at try{} catch{}?

   //your code here...
   //catch exception here... and do what ever you want with it.

There are plenty of exception handling articles and example on the web.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900