任何人都可以帮我解决这个错误“NullReferenceException未处理”?

时间:2017-04-21 11:42:53

标签: vb.net

我正在使用Visual Studio 2013,这部分代码是关于讲师发布咨询时间表。但是当我点击发布按钮时,它会显示我的错误。这是我的代码..

    Imports System.Data.OleDb

Public Class ConsultationSchedule

Private Sub publishbutton_Click(sender As Object, e As EventArgs) Handles publishbutton.Click
    Dim label As String = "Label"

    Dim sql As String
    Dim cmd As OleDbCommand

    Dim time1 As String = vbNull
    Dim time2 As String = vbNull
    Dim time3 As String = vbNull
    Dim time4 As String = vbNull

    Dim day1 As String = Nothing
    Dim day2 As String = vbNull
    Dim day3 As String = vbNull
    Dim day4 As String = vbNull
    Dim day5 As String = vbNull

    Dim available1 As String = Nothing
    Dim available2 As String = vbNull
    Dim available3 As String = vbNull
    Dim available4 As String = vbNull


    For i = 11 To 15
        time1 = Label7.Text

        For k = 16 To 20
            time2 = Label8.Text
            For t = 21 To 25
                time3 = Label9.Text
                For u = 26 To 30
                    time4 = Label10.Text
                Next
            Next
        Next
    Next

    For i = 11 To 30
        If (i = 11) Then
            day1 = Label2.Text
            If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                available1 = "Available"
            Else
                available1 = "Unavailable"
            End If

        ElseIf (i = 16) Then
            day1 = Label2.Text
            If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                available2 = "Available"
            Else
                available2 = "Unavailable"
            End If

        ElseIf (i = 21) Then
            day1 = Label2.Text
            If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                available3 = "Available"
            Else
                available3 = "Unavailable"
            End If

        ElseIf (i = 26) Then
            day1 = Label2.Text
            If (Me.Controls(label & i.ToString).BackColor = Color.Lime) Then
                available4 = "Available"
            Else
                available4 = "Unavailable"
            End If
        End If

    Next


    For k = 11 To 30
        If (k = 12) Then
            day2 = Label3.Text
        ElseIf (k = 17) Then
            day2 = Label3.Text
        ElseIf (k = 22) Then
            day2 = Label3.Text
        ElseIf (k = 27) Then
            day2 = Label3.Text
        End If

    Next

    For t = 11 To 30
        If (t = 13) Then
            day3 = Label4.Text
        ElseIf (t = 18) Then
            day3 = Label4.Text
        ElseIf (t = 23) Then
            day3 = Label4.Text
        ElseIf (t = 28) Then
            day3 = Label4.Text
        End If

    Next

    For u = 11 To 30
        If (u = 14) Then
            day4 = Label5.Text
        ElseIf (u = 19) Then
            day4 = Label5.Text
        ElseIf (u = 24) Then
            day4 = Label5.Text
        ElseIf (u = 29) Then
            day4 = Label5.Text
        End If

    Next

    For y = 11 To 30
        If (y = 15) Then
            day5 = Label6.Text
        ElseIf (y = 20) Then
            day5 = Label6.Text
        ElseIf (y = 25) Then
            day5 = Label6.Text
        ElseIf (y = 30) Then
            day5 = Label6.Text
        End If

    Next

    Dim conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=scheduledatabase.accdb;Persist Security Info=False;")
    sql = "INSERT INTO consultationschedule(Time, Day, Available)VALUES(@Time,@Day,@Available)"

    conn.Open()
    cmd = New OleDbCommand(sql, conn)
    cmd.Parameters.AddWithValue("@Time", time1)
    cmd.Parameters.AddWithValue("@Day", day1)
    cmd.Parameters.AddWithValue("@Available", available1)
    'cmd.ExecuteNonQuery()

    Dim f As Integer = cmd.ExecuteNonQuery()
    If (f >= 1) Then
        ToolStrip1.Text = f.ToString & " record added successfully"
    Else
        ToolStrip1.Text = "Unable to add record"

    End If
    MessageBox.Show("User Widrawed", "Widrawed", MessageBoxButtons.OK, MessageBoxIcon.Information)
    conn.Close()
End Sub

1 个答案:

答案 0 :(得分:2)

Me.Controls(label & i.ToString).BackColor = Color.Lime

相同
Me.Controls("Label11").BackColor = Color.Lime
...
Me.Controls("Label30").BackColor = Color.Lime

你有30个编号标签吗?

你可以这样做

If Not Me.Controls(label & i.ToString) Is Nothing Then
    Me.Controls(label & i.ToString).BackColor = Color.Lime
End If