English 中文(简体)
VB.NET 丢弃“ 对象引用未设置为对象实例” 例外
原标题:VB.NET throwing "Object reference not set to an instance of an object" Exception

为什么我得到这个例外?

对象引用未设置为对象实例。

与:

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

此处 wb webBrowser 控件的名称 。

这是所有代码:

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

<强 > 更新

正如在评论中建议的那样,我修改了这样的代码:

  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

我拿到了el 算不上什么 。 我现在要怎么解决这个问题?


或者如果你们能帮我 解决我的问题

< a href=" "https://stackoverflow.com/ questions/10767434/how-to-fill-html-form-using-web-browser-control" > 如何使用 Web 浏览器控制 填充 html 表格

最佳回答

我认为,这是一个很好的例子,说明为什么把业务分成几行是好的,而不是试图在一个行内进行许多业务,特别是当可以归还无效价值时。

如果您采用 wb.Document. getElementById (“ formfieldv1”) 。 InnerText = " some value" , 内输入 = " some value" 。

并分解成

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

当例外被抛出时,失败的会更加明显。 在通过代码时检查每次操作的结果也更容易。 从汇编的角度讲,它不会有什么区别,它最终会被汇编成同一个IL。

我认为有一种自然的愿望 尽可能多地在一行代码中做, 但我认为在许多情况下 它伤害了可读性和可调试性。

问题回答

暂无回答




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签