-1

i have this problem"error coding (Object reference not set to an instance of an object)" please help me and this my full coding.

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
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
jibal
  • 9
  • 4

2 Answers2

0

Since the error-causing line is in a Catch block and rdr is only set in the accompanying Try, there's no guarantee that rdr is ever initialized. Since the data schema is not using English (I think?), I'm not really sure what you're trying to accomplish with the handler. I recommend doing an If rdr Is Nothing and doing a separate error type for that before going into what you currently have in the Catch block.

And, as people pointed out in the comments, use the And operator (or AndAlso to replicate C# behavior) instead of &, which is the string concatenator in VB.NET.

Ben N
  • 2,883
  • 4
  • 26
  • 49
0

Look what you have going on here:

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

You've got 2 readers: Dim Reader As MySqlDataReader and Dim rdr As MySqlDataReader.

You do Reader = Command.ExecuteReader and a While Reader.Read and then immediately close it via Reader.Close(). That's problem 1. Problem 1a is that you are even looping through that first Reader. Why? You aren't doing anything with it?

Problem 2 is that your second Reader you do rdr = cmd.ExecuteReader yet you don't do anything with it. It seems to me that your If statement wasn't supposed to be inside of the Catch but underneath the rdr = cmd.ExecuteReader. Therefore, move your Catch down after the If statement where you are using the rdr object.

Problem 3 is your & inside the If statement. Those need to be changed to AndAlso.

Here is how I tried to salvage things:

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
Code Maverick
  • 20,171
  • 12
  • 62
  • 114
  • yeah. Lots of problems :) – HengChin Jul 24 '14 at 01:23
  • can you help me to resolve the problem?im a beginner programmer. :( – jibal Jul 24 '14 at 01:27
  • @jibal - I am really just unsure about what you are trying to do here. Your `If` statement I'm guessing wasn't totally finished? It's like you copied your condition and code blocks all the way down and only changed the `Form` number. I'm guessing you meant to increment the `spath` maybe, or the `keadaan` condition? You tell me. What is it that you are trying to do with that `If` statement? – Code Maverick Jul 24 '14 at 01:34
  • @CodeMaverick- let me explain about my if else statement .. ok i want display with other form. the form will change if keadaan =1 wil open other form if keadaan=0 will stay in form 1 from database. – jibal Jul 24 '14 at 01:51
  • @jibal - So what about `no = count`? – Code Maverick Jul 24 '14 at 01:59
  • what is the need for AndAlso in `If keadaan = 1 AndAlso no = count Then`, AND is enough na? may i know what is the difference between AndAlso & AND(Logical AND) – Suji Jul 24 '14 at 02:00
  • @CodeMaverick- count use for procced next row in database.. – jibal Jul 24 '14 at 02:02
  • 1
    @SujithKarivelil - `And` evaluates both sides of the condition, whereas `AndAlso` is a short-circuit and only evaluates the right side if the left side is true. Same is true for `Or` and `OrElse`. – Code Maverick Jul 24 '14 at 02:02
  • @SujithKarivelil my if else are wrong? – jibal Jul 24 '14 at 03:54
  • @ jibal : it gives the correct result but am not sure about the explanation. – Suji Jul 24 '14 at 04:02
  • the correct way to use AndAlso operator is : `Dim a, b As Integer` `a = 1` `b = 1` `If b AndAlso a = 1 Then` `MsgBox("If loop is correct")` `End If` – Suji Jul 24 '14 at 04:06
  • @SujithKarivelil - yeah but my if..else still not working – jibal Jul 24 '14 at 05:46
  • thanks guys now my program almost done.the problem are solve ...#freeGaza – jibal Jul 25 '14 at 00:45