错误:对象引用未设置为对象的实例 [英] Error :Object reference not set to an instance of an object

查看:89
本文介绍了错误:对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序:翻转图像"

来源:

I have a program : " flip images"

Source :

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



在此出现错误对象引用未设置为对象的实例":
danhsachbt(sott(2)).Visible = False
请帮助我!



There is an error "Object reference not set to an instance of an object" in this :
danhsachbt(sott(2)).Visible = False
Please help me !

推荐答案

问题与danhsachbt数组初始化有关.这是一个基于零索引的数组,您将从1开始初始化.

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

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



将上面的代码更改为



Change above code to

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



您还需要根据需要进行适当的更改,我的意思是现在您必须从0到5而不是1到6循环.

问候
鲁西(Rushi)



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


danhsachbt(sott(2)).Visible = False

这仅表示danhsachbt(sott(2))为空.您尝试对空对象使用属性,因此出现错误.

放入DEBUGGER并查看danhsachbt(sott(2))的值. sott(2)可能没有任何内容.您可以根据sott(2)的有效值(如果存在)处理该方案.
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.


这篇关于错误:对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆