Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program : " flip images"

Source :
VB
Imports System.IO
Public Class Form1
    Dim khoa As Boolean
    Dim danhsachbt(6) As Button
    Dim dshinh(6) As Image
    Dim ds1(6), ds2(6), clicks, sott(2), s As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        danhsachbt(1) = Button1
        danhsachbt(2) = Button2
        danhsachbt(3) = Button3
        danhsachbt(4) = Button4
        danhsachbt(5) = Button5
        danhsachbt(6) = Button6
        Label1.Text = 40
        NewGame()
        ''

    End Sub

    Public Sub Image()
        dshinh(ds1(1)) = My.Resources.a
     dshinh(ds1(2)) = My.Resources.a
     ds2(ds1(1)) = 1
        ds2(ds1(2)) = 1

        dshinh(ds1(3)) = My.Resources.b
dshinh(ds1(4)) = My.Resources.b
ds2(ds1(3)) = 2
        ds2(ds1(4)) = 2

        dshinh(ds1(5)) = My.Resources.c
 dshinh(ds1(6)) = My.Resources.c
ds2(ds1(5)) = 3
        ds2(ds1(6)) = 3

    End Sub
    Public Sub LoadBT()
        For i As Integer = 1 To 6
            danhsachbt(i).Visible = True
        Next
    End Sub
    Public Sub NewGame()
        Dim i, j, Rand As Integer
        clicks = 0
        For i = 1 To 6
            danhsachbt(i).Image = Nothing
        Next

        For i = 1 To 6
A1:
            Randomize()
            Rand = Int(6 * Rnd() + 1)

            'Kt neu co rui thi lam lai
            For j = 1 To i - 1
                If Rand = ds1(j) Then GoTo A1
            Next
            ds1(i) = Rand
        Next
        Image()
        LoadBT()
        ProgressBar1.Maximum = Label1.Text
        ProgressBar1.Value = ProgressBar1.Maximum

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If khoa = False Then Exit Sub
        If sott(1) = 1 Then Exit Sub

        clicks += 1
        sott(clicks) = 1
        Button1.Image = dshinh(1)
        If clicks = 2 Then Timer1.Enabled = True

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If khoa = False Then Exit Sub
        If sott(1) = 2 Then Exit Sub

        clicks += 1
        sott(clicks) = 2
        Button2.Image = dshinh(2)
        If clicks = 2 Then Timer1.Enabled = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If khoa = False Then Exit Sub
        If sott(1) = 3 Then Exit Sub

        clicks += 1
        sott(clicks) = 3
        Button3.Image = dshinh(3)
        If clicks = 2 Then Timer1.Enabled = True
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If khoa = False Then Exit Sub
        If sott(1) = 4 Then Exit Sub

        clicks += 1
        sott(clicks) = 4
        Button4.Image = dshinh(4)
        If clicks = 2 Then Timer1.Enabled = True
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If khoa = False Then Exit Sub
        If sott(1) = 5 Then Exit Sub

        clicks += 1
        sott(clicks) = 5
        Button5.Image = dshinh(5)
        If clicks = 2 Then Timer1.Enabled = True
    End Sub
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If khoa = False Then Exit Sub
        If sott(1) = 6 Then Exit Sub

        clicks += 1
        sott(clicks) = 6
        Button6.Image = dshinh(6)
        If clicks = 2 Then Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If clicks = 1 Then Exit Sub
        'KT hinh giong nhau
        If ds2(sott(1)) = ds2(sott(2)) Then
            danhsachbt(sott(2)).Visible = False
            danhsachbt(sott(1)).Visible = False
            MsgBox("co gang")
            'KT ket thuc game
            Dim i As Integer
            Dim Match As Boolean = True
            For i = 1 To 6
                If danhsachbt(i).Visible = True Then
                    Match = False
                    Exit For
                End If
            Next
            If Match = True And TextBox1.Text > 0 Then
                Timer2.Stop()
                ProgressBar1.Visible = True
                MsgBox("Chuc Mung! Ban Da Chien Thang", , "Thong Bao")
            End If
        Else
            danhsachbt(sott(1)).Image = Nothing
            danhsachbt(sott(2)).Image = Nothing
        End If
        sott(1) = 0
        sott(2) = 0
        clicks = 0
    End Sub
    Dim check As Boolean
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        ProgressBar1.Visible = False
        TextBox1.Text = Label1.Text
        s = TextBox1.Text
        khoa = True
        NewGame()
        Timer2.Start()
        check = True

    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        TextBox1.Text = s - 1
        If ProgressBar1.Value <= ProgressBar1.Maximum And ProgressBar1.Value > 0 Then
            ProgressBar1.Value -= 1
        Else
            ProgressBar1.Value = 0
        End If
        s = s - 1
        If s = 0 Then
            Timer2.Stop()
            ProgressBar1.Visible = True
            MsgBox("Het Thoi Gian! Ban Da Thua", , "Thong Bao")
            khoa = False
        End If
    End Sub
End Class


There is an error "Object reference not set to an instance of an object" in this :
danhsachbt(sott(2)).Visible = False
Please help me !
Posted
Updated 16-May-11 21:12pm
v2

Problem is with danhsachbt array initialization. It's a Zero Index based array and you are initializing from 1.

VB
danhsachbt(1) = Button1
danhsachbt(2) = Button2
danhsachbt(3) = Button3
danhsachbt(4) = Button4
danhsachbt(5) = Button5
danhsachbt(6) = Button6


Change above code to

VB
danhsachbt(0) = Button6
danhsachbt(1) = Button1
danhsachbt(2) = Button2
danhsachbt(3) = Button3
danhsachbt(4) = Button4
danhsachbt(5) = Button5


you also need to make appropriate changes where required, I mean now you have to loop from 0 to 5 not 1 to 6.

Regards
Rushi
 
Share this answer
 
Comments
DishR 17-May-11 3:46am    
Good Catch
danhsachbt(sott(2)).Visible = False

This simply means danhsachbt(sott(2)) is null. You tried using a property to a null object, hence the error.

Put DEBUGGER and see the value of danhsachbt(sott(2)). May be there is nothing for sott(2). You can handle the scenario as per the valid values of sott(2), if it exists.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-May-11 3:15am    
My 5.
--SA
Sandeep Mewara 17-May-11 3:16am    
Thanks SA.
Joshi, Rushikesh 17-May-11 3:48am    
danhsachbt(sott(2)) is null because sott(2) has "0" value and there is nothing stored in danhsachbt 0th place.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900