Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been trying to change the HTML code of a select tag in an HTML code but whenever I do that the

"Object reference not set to an instance of an object"

error occurs.This is the C# code:
C#
public void SetDefaultValue(string ControlID,string Value)
 {
HtmlDocument doc = webBrowser1.Document;
HtmlElement list = doc.GetElementById(ControlID);
list.OuterHTML+= Value;         
 }
private void Form1_Load(object sender, EventArgs e)
{            
    htmlLoadWebbrowser1.SetDefaultValue("D8876943-5861-4D62-9249-C5FEF88219FA", "<SELECT id=\"D8876943-5861-4D62-9249-C5FEF88219FA\" multiple size=\"4\" name=\"drop1\"> <OPTION value=\"3\">item 3</OPTION></SELECT>");            
}

The function is supposed to change the code of an select tag in an HTML page,it receives the ID of the control by "controlID" and generates the inner code by "Value". I suspect that I need to call the function elsewhere,the problem is that I do not know where. Thank you.
Posted
Updated 27-Apr-13 0:06am
v2

1 solution

Object reference not set to an instance of an object

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.
 
Share this answer
 
Comments
pedramham 27-Apr-13 6:38am    
@Sandeep:Thank you but my problem is something else.
My code will work like this:
private void Form1_Load(object sender, EventArgs e)
{
GenerateHTMLClass gnh = new GenerateHTMLClass(@"D:\test2.xml", @"D:\test.htm");
htmlLoadWebbrowser1.LoadHTML(@"D:\test.htm");
MessageBox.Show("");
htmlLoadWebbrowser1.SetListboxHTML("D8876943-5861-4D62-9249-C5FEF88219FA", "<SELECT id=D8876943-5861-4D62-9249-C5FEF88219FA title=\"Type of property\" name=PropertyType type=\"select\" value=\"\" width=\"10\"><OPTION value=\"1\">item 1</OPTION><OPTION value=\"2\">item 2</OPTION><OPTION value=\"3\">item 3</OPTION></SELECT>");
}

I mean when I add messagebox.show() to my code it works.
What can I do to get it done without messagebox
Sandeep Mewara 27-Apr-13 6:50am    
I don't see the information that 'it works' when you add messagebox shared before.

Now, if you say that it works with above code and not with code in question, then it must be some race condition. Try to put some delay/sleep and see.

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