2014-02-19 159 views
-1

當我去調試我的項目在Visual Basic中,我得到它說的錯誤消息:VB調試錯誤

An unhandled exception of type 'System.InvalidOperationException' occurred in Name Database.exe

Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object.

這裏是我的代碼:

Public Class Form1 
Dim member As String = TextBoxName.Text 
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click 
    ListBoxName.Items.Add(member & " " & TextBoxDate.Text) 
    TextBoxDate.Text = "" 
    TextBoxName.Text = "" 
End Sub 

Private Sub ButtonRemove_Click(sender As Object, e As EventArgs) Handles ButtonRemove.Click 
    ListBoxName.Items.Remove(ListBoxName.SelectedItem) 
End Sub 

Private Sub ButtonCheck_Click(sender As Object, e As EventArgs) Handles ButtonCheck.Click 
    If ListBoxName.Items.Contains(member & " " & Date.Today.ToString) Then 
     MsgBox(ListBoxName.Items.Contains(Date.Today.ToString) & "membership needs to be renewed") 
    End If 
End Sub 
End Class 
+0

讀取內部異常堆棧跟蹤。 – SLaks

+0

這可能是錯誤:'Dim member As String = TextBoxName.Text'表單尚未創建(不是任何控件),但是您引用了其中一個控件(這不是任何控件)。聲明它,但不要初始化('member = ...'),直到FormLoad – Plutonix

+0

什麼是「內部異常堆棧跟蹤」? – user3267723

回答

1
Dim member As String = TextBoxName.Text 

這不能工作,TextBoxName變量在執行此分配時尚未初始化。您必須使用如下構造函數:

Dim member As String 

Public Sub New() 
    InitializeComponent() 
    member = TextBoxName.Text 
End Sub 

它是設置TextBoxName控件引用的InitializeComponent()。請注意,它仍然不太可能工作,Text屬性不會被用戶設置。所以會員將包含一個空字符串。除非你給設計師一個價值。這表明這更可能是正確的:

Dim member As String = String.Empty