错误编码(对象引用未设置为对象的实例)

时间:2014-07-24 00:57:01

标签: vb.net

我有这个问题"错误编码(对象引用未设置为对象的实例)"请帮助我,这是我的完整编码。

Imports MySql.Data.MySqlClient
Public Class Form1
    Dim MysqlConn As MySqlConnection
    Dim Command As MySqlCommand
    Dim Reader As MySqlDataReader
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Sda As New MySqlDataAdapter
        Try
            If Button1.BackColor = Color.Lime Then
                Me.Button1.BackColor = Color.Lime
            End If
            MysqlConn.Open()
            Dim query As String
            query = "select * from info.penjara"
            query = "update penjara.info set keadaan =1 where no =1 "
            Command = New MySqlCommand(query, MysqlConn)
            Reader = Command.ExecuteReader()
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()

            Dim spath As String
            Dim mysound As Media.SoundPlayer

            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            If Button1.BackColor = Color.Lime Then
                mysound.PlayLooping()
            End If
            Form2.Show()
            Me.Hide()

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Sda As New MySqlDataAdapter
        Try
            If Button2.BackColor = Color.Lime Then
                Me.Button2.BackColor = Color.Lime
            End If
            MysqlConn.Open()
            Dim query As String
            query = "select * from info.penjara"
            query = "update penjara.info set keadaan =1 where no =2 "

            Command = New MySqlCommand(query, MysqlConn)
            Reader = Command.ExecuteReader()
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()

            Dim spath As String
            Dim mysound As Media.SoundPlayer

            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            If Button2.BackColor = Color.Lime Then
                mysound.PlayLooping()
            End If
            Form3.Show()
            Me.Hide()
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Sda As New MySqlDataAdapter
        Try
            If Button3.BackColor = Color.Lime Then
                Me.Button3.BackColor = Color.Lime
            End If
            MysqlConn.Open()
            Dim query As String
            query = "select * from info.penjara"
            query = "update penjara.info set keadaan =1 where no =3 "
            Command = New MySqlCommand(query, MysqlConn)
            Reader = Command.ExecuteReader()
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()

            Dim spath As String
            Dim mysound As Media.SoundPlayer

            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            If Button3.BackColor = Color.Lime Then
                mysound.PlayLooping()
            End If
            Form4.Show()
            Me.Hide()
        End Try
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Sda As New MySqlDataAdapter
        Try
            If Button4.BackColor = Color.Lime Then
                Me.Button4.BackColor = Color.Lime
            End If
            MysqlConn.Open()
            Dim query As String
            query = "select * from info.penjara"
            query = "update penjara.info set keadaan =1 where no =4 "
            Command = New MySqlCommand(query, MysqlConn)
            Reader = Command.ExecuteReader()
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()

            Dim spath As String
            Dim mysound As Media.SoundPlayer

            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            If Button4.BackColor = Color.Lime Then
                mysound.PlayLooping()
            End If
            Form5.Show()
            Me.Hide()
        End Try
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Sda As New MySqlDataAdapter
        Try
            If Button5.BackColor = Color.Lime Then
                Me.Button5.BackColor = Color.Lime
            End If
            MysqlConn.Open()
            Dim query As String
            query = "select * from info.penjara"
            query = "update penjara.info set keadaan =1 where no =5 "
            Command = New MySqlCommand(query, MysqlConn)
            Reader = Command.ExecuteReader()
            MysqlConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
            MysqlConn.Dispose()

            Dim spath As String
            Dim mysound As Media.SoundPlayer

            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            If Button5.BackColor = Color.Lime Then
                mysound.PlayLooping()
            End If
            Form6.Show()
            Me.Hide()
        End Try
    End Sub

    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        MysqlConn = New MySqlConnection
        MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
        Dim Reader As MySqlDataReader

        MysqlConn.Open()

        Dim query As String
        Dim spath As String
        Dim count As Integer = 0
        Dim mysound As Media.SoundPlayer
        Dim cmd As MySqlCommand
        Dim rdr As MySqlDataReader


        query = "Select * from penjara.info"
        Command = New MySqlCommand(query, MysqlConn)
        Reader = Command.ExecuteReader
        While Reader.Read
            Try
                Reader.Close()
                count = count + 1
                query = "Select penjara.info where  no = '" & count & "'"
                cmd = New MySqlCommand(query, MysqlConn)
                rdr = cmd.ExecuteReader
            Catch ex As Exception


                If (rdr.GetInt32("keadaan") = 1) & (rdr.GetInt32("no") = count) Then
                    Form2.Show()
                    Me.Hide()
                    spath = "C:\Users\User\Music\emergency003.wav"
                    mysound = New Media.SoundPlayer(spath)
                    mysound.PlayLooping()
                ElseIf (rdr.GetInt32("keadaan") = 1) & (rdr.GetInt32("no") = count) Then
                    Form3.Show()
                    Me.Hide()
                    spath = "C:\Users\User\Music\emergency003.wav"
                    mysound = New Media.SoundPlayer(spath)
                    mysound.PlayLooping()
                ElseIf (rdr.GetInt32("keadaan") = 1) & (rdr.GetInt32("no") = count) Then
                    Form4.Show()
                    Me.Hide()
                    spath = "C:\Users\User\Music\emergency003.wav"
                    mysound = New Media.SoundPlayer(spath)
                    mysound.PlayLooping()
                ElseIf (rdr.GetInt32("keadaan") = 1) & (rdr.GetInt32("no") = count) Then
                    Form5.Show()
                    Me.Hide()
                    spath = "C:\Users\User\Music\emergency003.wav"
                    mysound = New Media.SoundPlayer(spath)
                    mysound.PlayLooping()
                ElseIf (rdr.GetInt32("keadaan") = 1) & (rdr.GetInt32("no") = count) Then
                    Form5.Show()
                    Me.Hide()
                    spath = "C:\Users\User\Music\emergency003.wav"
                    mysound = New Media.SoundPlayer(spath)
                    mysound.PlayLooping()
                End If
            End Try
        End While
    End Sub

End Class

2 个答案:

答案 0 :(得分:0)

由于导致错误的行位于Catch块中,rdr仅在随附的Try中设置,因此无法保证rdr已初始化。由于数据模式不使用英语(我认为?),我不确定你要用处理程序完成什么。我建议您在进入If rdr Is Nothing块中的当前内容之前执行Catch并为其执行单独的错误类型。

正如人们在评论中指出的那样,使用And运算符(或AndAlso来复制C#行为)而不是&,这是VB.NET中的字符串连接符

答案 1 :(得分:0)

看看你在这里发生了什么:

MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=;database=penjara"
Dim Reader As MySqlDataReader

MysqlConn.Open()

Dim query As String
Dim spath As String
Dim count As Integer = 0
Dim mysound As Media.SoundPlayer
Dim cmd As MySqlCommand
Dim rdr As MySqlDataReader

query = "Select * from penjara.info"
Command = New MySqlCommand(query, MysqlConn)
Reader = Command.ExecuteReader
While Reader.Read
    Try
        Reader.Close()
        count = count + 1
        query = "Select penjara.info where  no = '" & count & "'"
        cmd = New MySqlCommand(query, MysqlConn)
        rdr = cmd.ExecuteReader

您有2位读者:Dim Reader As MySqlDataReaderDim rdr As MySqlDataReader

您执行Reader = Command.ExecuteReaderWhile Reader.Read ,然后立即通过Reader.Close() 关闭它。这是问题1.问题1a是你甚至循环通过第一个读者。为什么?你没有做任何事情吗?

问题2是你做的第二个读者rdr = cmd.ExecuteReader但你没有做任何事情。在我看来,您的If声明不应该在Catch内,而应在rdr = cmd.ExecuteReader下面。因此,在使用Catch对象的If语句后,将rdr向下移动

问题3是&语句中的If。这些需要更改为AndAlso

以下是我试图挽救的事情:

Try

    count = count + 1
    query = "Select penjara.info where  no = '" & count & "'"
    cmd = New MySqlCommand(query, MysqlConn)
    rdr = cmd.ExecuteReader

    While rdr.Read

        Dim keadaan = rdr.GetInt32("keadaan")
        Dim no = rdr.GetInt32("no")

        If keadaan = 1 AndAlso no = count Then

            Form2.Show()
            Me.Hide()
            spath = "C:\Users\User\Music\emergency003.wav"
            mysound = New Media.SoundPlayer(spath)
            mysound.PlayLooping()

        ElseIf 'insert all the other conditions here ...

        End If

    End While

Catch ex As Exception

    'TODO: Handle exception.

End Try