0

Why do I get this Exception?

Object reference not set to an instance of an object.

With:

wb.Document.GetElementById("formfieldv1").InnerText = "some value"

Where wb is the name of the WebBrowser control.

Here is all the code:

Private Sub btnSend_Click(sender As System.Object, e As System.EventArgs) Handles btnSend.Click
Dim strFrom As String = txtFrom.Text
Dim strTo As String = txtTo.Text
Dim strMsg As String = txtMsg.Text
wb.Document.GetElementById("formfieldv1").InnerText = strFrom ' strFrom fills fine
End Sub

Update

As suggested in comments I modified code like this:

  Dim doc = wb.Document

  If (doc Is Nothing) Then
     MsgBox("doc is nothing")
  End If


  Dim el = wb.Document.GetElementById("formfieldv1")

  If (el Is Nothing) Then
     MsgBox("el is nothing")
  Else
     el.InnerText = strFrom
  End If

With that I am getting el is nothing. How do I solve this now ?


Alternatively if you guys can help me out with this that will solve my problem too:

How to fill html form using web browser control

14
  • 2
    Either wb is Nothing, the Document is Nothing, or a formfieldv1 element does not exist in the document. Commented May 26, 2012 at 17:07
  • Break it down in steps. Either Document or the result of GetElementById() is null. Commented May 26, 2012 at 17:07
  • @Oded: The wb is dropped on form and I can see its members when typing it. Also formfieldv1 is there as can be seen here freesmscraze.com for From field Commented May 26, 2012 at 17:10
  • As @Henk suggested, you should break down each part of that line - assign wb.Document to a variable. Then call GetElementById on that, assigning the result to an element, then set InnerText on this variable. One of these will be Nothing. Commented May 26, 2012 at 17:12
  • @Oded: The el here turns Nothing: Dim el = wb.Document.GetElementById("formfieldv1") How do I solve this plz ? Commented May 26, 2012 at 17:22

1 Answer 1

2

I think this is a great example of why it's good to break down operations into several lines, instead of trying to do many operations in one line, especially when null values can be returned.

If you take wb.Document.GetElementById("formfieldv1").InnerText = "some value"

and break it down into

var document = wb.Document;
var element = document.GetElementById("formfieldv1");
element.InnerText = "some value";

When the exception is thrown, it will be much more apparent what is failing. It is also easier to inspect the result of each operation when stepping through code. From a compilation standpoint, it will make no difference, it will ultimately be compiled down to the same IL.

I think there tends to be a natural desire to do as much as possible in a single line of code, but I think in many cases it hurts readability and debug-ability.

Sign up to request clarification or add additional context in comments.

1 Comment

There was an iframe actually which i did not notice, thanks to Tony who spotted that in comments above. Thanks for your answer though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.