[RESOLVED] [2005] NullReference was unhandlded
Hi guys
I am having problem to fills the text on each textbox in the webpage. I did get one textbox to fill only, not for others. When I attempt to fill the texts in two textbox at the same time, I get the error.
Error:
NullReference was unhandlded
Object reference not set to an instance of an object.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Document.Forms(0).All("Username").SetAttribute("value", "fake")
WebBrowser1.Document.Forms(0).All("Password").SetAttribute("value", "password")
WebBrowser1.Document.Forms(0).All("Confirm Password").SetAttribute("value", "password")
WebBrowser1.Document.Forms(0).All("Email Address").SetAttribute("value", "[email protected]")
WebBrowser1.Document.Forms(0).All("Confirm Email Address").SetAttribute("value", "[email protected]")
WebBrowser1.Document.Forms(0).InvokeMember("submit")
End Sub
I don't understand it as it should work in the first place so please can you help me to get this fix??
Thanks,
Mark
Re: [2005] NullReference was unhandlded
Are "Confirm Password", "Email Address" and "Confirm Email Address" valid element ID's? (element ID's are not allowed to have spaces in them as far as I know)
Please also post the html code of the web page.
Re: [2005] NullReference was unhandlded
Here it is:
Code:
<table border="0" cellpadding="5" cellspacing="0" width="95%">
<tbody><tr>
<td class="midinfo">
<div class="heading">Register</div>
<p>
<br>
<font color="#9c1515">*</font> Username:<br>
<input name="username" size="25" maxlength="15" class="form" autocomplete="off" value="" type="text">
<br>
<br>
<font color="#9c1515">*</font> Password:<br>
<input name="pass" size="25" class="form" autocomplete="off" value="" type="password">
<br>
<br>
<font color="#9c1515">*</font> Confirm Password:<br>
<input name="verpass" size="25" class="form" autocomplete="off" value="" onpaste="event.returnValue=false;" type="password">
<br>
<br>
<font color="#9c1515">*</font> Email Address:<br>
<input name="email" size="25" class="form" autocomplete="off" value="" type="text">
<br>
<br>
<font color="#9c1515">*</font> Confirm Email Address:<br>
<input name="veremail" size="25" class="form" autocomplete="off" value="" onpaste="event.returnValue=false;" type="text">
<br>
<br>
<font color="#9c1515">*</font> Pay E-mail Address:<br>
<input name="paypal" size="25" class="form" autocomplete="off" value="" type="text">
<br>
<br>
<font color="#9c1515">*</font> Your Country:<br>
<select name="country" class="form">
<option value="none">--- Select Country ---</option>
<option value="1">United States</option><option value="2">Albania</option><option value="3">Algeria</option><option value="4">Andorra</option><option value="5">Angola</option><option value="6">Anguilla</option><option value="7">Antigua and Barbuda</option><option value="8">Argentina</option><option value="9">Armenia</option><option value="10">Aruba</option><option value="11">Australia</option><option value="12">Austria</option><option value="13">Azerbaijan Republic</option><option value="14">Bahamas</option><option value="15">Bahrain</option><option value="16">Barbados</option><option value="17">Belgium</option><option value="18">Belize</option><option value="19">Benin</option><option value="20">Bermuda</option><option value="21">Bhutan</option><option value="22">Bolivia</option><option value="23">Bosnia and Herzegovina</option><option value="24">Botswana</option><option value="25">Brazil</option><option value="26">British Virgin Islands</option><option value="27">Brunei</option><option value="28">Bulgaria</option><option value="29">Burkina Faso</option><option value="30">Burundi</option><option value="31">Cambodia</option><option value="32">Canada</option><option value="33">Cape Verde</option><option value="34">Cayman Islands</option><option value="35">Chad</option><option value="36">Chile</option><option value="37">China Worldwide</option><option value="38">Colombia</option><option value="39">Comoros</option><option value="40">Cook Islands</option><option value="41">Costa Rica</option><option value="42">Croatia</option><option value="43">Cyprus</option><option value="44">Czech Republic</option><option value="45">Democratic Republic of the Congo</option><option value="46">Denmark</option><option value="47">Djibouti</option><option value="48">Dominica</option><option value="49">Dominican Republic</option><option value="50">Ecuador</option><option value="51">El Salvador</option><option value="52">Eritrea</option><option value="53">Estonia</option><option value="54">Ethiopia</option><option value="55">Falkland Islands</option><option value="56">Faroe Islands</option><option value="57">Federated States of Micronesia</option><option value="58">Fiji</option><option value="59">Finland</option><option value="60">France</option><option value="61">French Guiana</option><option value="62">French Polynesia</option><option value="63">Gabon Republic</option><option value="64">Gambia</option><option value="65">Germany</option><option value="66">Gibraltar</option><option value="67">Greece</option><option value="68">Greenland</option><option value="69">Grenada</option><option value="70">Guadeloupe</option><option value="71">Guatemala</option><option value="72">Guinea</option><option value="73">Guinea Bissau</option><option value="74">Guyana</option><option value="75">Honduras</option><option value="76">Hong Kong</option><option value="77">Hungary</option><option value="78">Iceland</option><option value="79">India</option><option value="80">Indonesia</option><option value="81">Ireland</option><option value="82">Israel</option><option value="83">Italy</option><option value="84">Jamaica</option><option value="85">Japan</option><option value="86">Jordan</option><option value="87">Kazakhstan</option><option value="88">Kenya</option><option value="89">Kiribati</option><option value="90">Kuwait</option><option value="91">Kyrgyzstan</option><option value="92">Laos</option><option value="93">Latvia</option><option value="94">Lesotho</option><option value="95">Liechtenstein</option><option value="96">Lithuania</option><option value="97">Luxembourg</option><option value="98">Madagascar</option><option value="99">Malawi</option><option value="100">Malaysia</option><option value="101">Maldives</option><option value="102">Mali</option><option value="103">Malta</option><option value="104">Marshall Islands</option><option value="105">Martinique</option><option value="106">Mauritania</option><option value="107">Mauritius</option><option value="108">Mayotte</option><option value="109">Mexico</option><option value="110">Mongolia</option><option value="111">Montserrat</option><option value="112">Morocco</option><option value="113">Mozambique</option><option value="114">Namibia</option><option value="115">Nauru</option><option value="116">Nepal</option><option value="117">Netherlands</option><option value="118">Netherlands Antilles</option><option value="119">New Caledonia</option><option value="120">New Zealand</option><option value="121">Nicaragua</option><option value="122">Niger</option><option value="123">Niue</option><option value="124">Norfolk Island</option><option value="125">Norway</option><option value="126">Oman</option><option value="127">Palau</option><option value="128">Panama</option><option value="129">Papua New Guinea</option><option value="130">Peru</option><option value="131">Philippines</option><option value="132">Pitcairn Islands</option><option value="133">Poland</option><option value="134">Portugal</option><option value="135">Qatar</option><option value="136">Republic of the Congo</option><option value="137">Reunion</option><option value="138">Romania</option><option value="139">Russia</option><option value="140">Rwanda</option><option value="141">Saint Vincent and the Grenadines</option><option value="142">Samoa</option><option value="143">San Marino</option><option value="144">São Tomé and Príncipe</option><option value="145">Saudi Arabia</option><option value="146">Senegal</option><option value="147">Seychelles</option><option value="148">Sierra Leone</option><option value="149">Singapore</option><option value="150">Slovakia</option><option value="151">Slovenia</option><option value="152">Solomon Islands</option><option value="153">Somalia</option><option value="154">South Africa</option><option value="155">South Korea</option><option value="156">Spain</option><option value="157">Sri Lanka</option><option value="158">St. Helena</option><option value="159">St. Kitts and Nevis</option><option value="160">St. Lucia</option><option value="161">St. Pierre and Miquelon</option><option value="162">Suriname</option><option value="163">Svalbard and Jan Mayen Islands</option><option value="164">Swaziland</option><option value="165">Sweden</option><option value="166">Switzerland</option><option value="167">Taiwan</option><option value="168">Tajikistan</option><option value="169">Tanzania</option><option value="170">Thailand</option><option value="171">Togo</option><option value="172">Tonga</option><option value="173">Trinidad and Tobago</option><option value="174">Tunisia</option><option value="175">Turkey</option><option value="176">Turkmenistan</option><option value="177">Turks and Caicos Islands</option><option value="178">Tuvalu</option><option value="179">Uganda</option><option value="180">Ukraine</option><option value="181">United Arab Emirates</option><option value="182">United Kingdom</option><option value="183">Uruguay</option><option value="184">Vanuatu</option><option value="185">Vatican City State</option><option value="186">Venezuela</option><option value="187">Vietnam</option><option value="188">Wallis and Futuna Islands</option><option value="189">Yemen</option><option value="190">Zambia</option><option value="191">West Africa</option><option value="192">Macedonia</option><option value="193">Egypt</option><option value="194">Pakistan</option><option value="195">Bangladesh</option><option value="196">Serbia</option><option value="197">Montenegro</option><option value="198">Yugoslavia</option><option value="199">Uzbekistan</option><option value="200">Ghana</option> </select>
<br>
<br>
Your name<br>
<input name="r" size="25" class="form" autocomplete="off" value="" type="text">
<br>
<br>
</p>
Your name<br>
<input name="r" size="25" class="form" autocomplete="off" value="" type="text">
<br>
<br>
</p>
<p>
<input name="chkAccept" value="1" type="checkbox">
I also want to tick the checkbox as well but I don't know how to do it in the webbrowser control.
Thanks,
Mark
Re: [2005] NullReference was unhandlded
OK... I'm right about that... You specified the wrong element names and thus the code returns nothing on this part WebBrowser1.Document.Forms(0).All("Username"). You then further tried to call the method SetAttribute on an object that is Nothing, and that's when you have the null exception.
Look at the html code, you should be using those names where it says name="blablabla". So your "Password" should be "pass" and so on....
Re: [2005] NullReference was unhandlded
Thanks for your help!!!! Please can you also help me that I want to tick the checkbox on the webpage but the code didn't tick the checkbox.
Code:
Dim doc As HtmlDocument
Dim elem As HtmlElement
doc = WebBrowser1.Document
For Each elem In doc.All
If elem.GetAttribute("type") = "chkAccept" Then
elem.SetAttribute("CHECKED", "CHECKED")
End If
Next
Thanks,
Mark
Re: [2005] NullReference was unhandlded
Shouldn't it be
Code:
If elem.GetAttribute("name") = "chkAccept" Then
elem.SetAttribute("checked", "checked")
End If
Re: [2005] NullReference was unhandlded
Quote:
Originally Posted by stanav
Shouldn't it be
Code:
If elem.GetAttribute("name") = "chkAccept" Then
elem.SetAttribute("checked", "checked")
End If
yes ;) or "id" should work
Re: [2005] NullReference was unhandlded
Thanks, what about the selecting the item on combobox in the webpage??
Thanks,
Mark
Re: [2005] NullReference was unhandlded
please can someone help me????
Re: [2005] NullReference was unhandlded
Well, try something like this:
Code:
'Get the document
Dim doc As HtmlDocument = WB.Document
'Get the select element (combobox) by its ID
Dim cbo As HtmlElement = doc.GetElementById("PutTheIdHere")
If cbo IsNot Nothing Then
'Get the items in the select element
Dim items As HtmlElementCollection = cbo.GetElementsByTagName("option")
'Loop thru the collection and find the item you need by its value
For Each itm As HtmlElement In items
'For example, we'll set selected to the 3rd item in the list
If itm.GetAttribute("value") = "3" Then
itm.SetAttribute("selected", "selected")
Exit For
End If
Next
End If
Re: [2005] NullReference was unhandlded
Thanks, but i have the error on my description list using with this statement.
Code:
Dim doc As HtmlDocument = WB.Document
The error message I get Name 'WB' is not declared.
Hope we can situations and hopefully it can be fix.
Thanks,
Mark
Re: [2005] NullReference was unhandlded
Aren't you calling it "WebBrowser1" in one of your previous posts.
Don't just copy and paste code given to you from here and expect it to always work. People providing you help don not know what you are calling your forms or controls, so they use generic names.
Re: [2005] NullReference was unhandlded
yes i am. I still get the error, do you know how to solve it??
Thanks,
Mark
Re: [2005] NullReference was unhandlded
Then you need to call it "WebBrowser1" or whatever you declared it initially everytime you refer to it. That is the only way to get rid of the error "Name 'WB' is not declared"
Re: [2005] NullReference was unhandlded
Thanks for the help!!!!!!!!
Keep up the good work as it now resolved :thumb:
Re: [RESOLVED] [2005] NullReference was unhandlded
Just curious, how did you resolve it?
Re: [RESOLVED] [2005] NullReference was unhandlded
Rename from Wb to webbrowser1 :)
Re: [RESOLVED] [2005] NullReference was unhandlded
You've got to be kidding, Mark... After 235 posts, you should well know that example codes are not meant for copy and paste and expect them to work as is... At the very least, you should change the variable names in the example to match what you're calling them.
Re: [RESOLVED] [2005] NullReference was unhandlded
stanav,
Trust me, he's not kidding.
Kerry Moorman
Re: [RESOLVED] [2005] NullReference was unhandlded
Yes regard posts #12 and #14.