VB.NET - 当我使用来自不同表单的文本时,NullReferenceError未处理

时间:2014-07-25 11:17:27

标签: vb.net vb.net-2010

我正在尝试创建一个程序,人们可以将文本上传到数据库。我正在使用WebBrowser来实现这一点,但是当代码尝试使用其他表单中的文本时,当我尝试在调试中运行代码时,它创建了一个NullReferenceError未处理。

这是上传时我在表单中的当前代码:

Public Class Form2
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://database.insma.org/")
        WebBrowser1.AllowNavigation = True
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("name").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("pass").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("edit-submit--2").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        WebBrowser1.AllowNavigation = True
        WebBrowser1.Navigate("http://database.insma.org/?q=node/add/log")
        Threading.Thread.Sleep(1000)
        WebBrowser1.Document.GetElementById("title").SetAttribute("value", Form1.TextBox2.Text)
        WebBrowser1.Document.GetElementById("field_time_utc_[und][0][value]").SetAttribute("value", Form1.TextBox4.Text)
        WebBrowser1.Document.GetElementById("field_location_ex_utwente_hong_k[und][0][value]").SetAttribute("value", Form1.TextBox5.Text)
        WebBrowser1.Document.GetElementById("field_frequency[und][0][value]").SetAttribute("value", Form1.TextBox3.Text)
        WebBrowser1.Document.GetElementById("field_id[und][0][value]").SetAttribute("value", Form1.TextBox6.Text)
        WebBrowser1.Document.GetElementById("field_call_up[und][0][value]").SetAttribute("value", Form1.TextBox7.Text)
        WebBrowser1.Document.GetElementById("field_group_count[und][0][value]").SetAttribute("value", Form1.TextBox8.Text)
        WebBrowser1.Document.GetElementById("body[und][0][value]").SetAttribute("value", Form1.TextBox1.Text)
        WebBrowser1.Document.GetElementById("edit-submit").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        MsgBox("Uploaded to the database!")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub
End Class

这就是错误的样子:http://i.imgur.com/LCQQCcy.png

如果你在解释发生了什么事情时,如果你愚蠢地告诉我,我会非常感激,因为我可能不明白你们要做什么。

如果您向我提供错误解决方案,那也很棒!

谢谢, 加权立方体

1 个答案:

答案 0 :(得分:0)

在您的班级实例下面,写下:

Public Class Form2
    Dim Form1 As New Form1()
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://database.insma.org/")
        WebBrowser1.AllowNavigation = True
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("name").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("pass").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("edit-submit--2").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        WebBrowser1.AllowNavigation = True
        WebBrowser1.Navigate("http://database.insma.org/?q=node/add/log")
        Threading.Thread.Sleep(1000)
        WebBrowser1.Document.GetElementById("title").SetAttribute("value", Form1.TextBox2.Text)
        WebBrowser1.Document.GetElementById("field_time_utc_[und][0][value]").SetAttribute("value", Form1.TextBox4.Text)
        WebBrowser1.Document.GetElementById("field_location_ex_utwente_hong_k[und][0][value]").SetAttribute("value", Form1.TextBox5.Text)
        WebBrowser1.Document.GetElementById("field_frequency[und][0][value]").SetAttribute("value", Form1.TextBox3.Text)
        WebBrowser1.Document.GetElementById("field_id[und][0][value]").SetAttribute("value", Form1.TextBox6.Text)
        WebBrowser1.Document.GetElementById("field_call_up[und][0][value]").SetAttribute("value", Form1.TextBox7.Text)
        WebBrowser1.Document.GetElementById("field_group_count[und][0][value]").SetAttribute("value", Form1.TextBox8.Text)
        WebBrowser1.Document.GetElementById("body[und][0][value]").SetAttribute("value", Form1.TextBox1.Text)
        WebBrowser1.Document.GetElementById("edit-submit").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        MsgBox("Uploaded to the database!")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub
End Class