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:
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
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.