Vb.net 未处理空引用异常

Vb.net 未处理空引用异常,vb.net,exception,reference,null,Vb.net,Exception,Reference,Null,这是一个非常简单的代码。我刚刚开始使用vb。我在做一个游戏,你要用最少的时间和最少的点击量来解决一个难题。每次我调试程序时,它都会突出显示单击和最终时间,并表示未处理空引用异常。我在全球范围内宣布了点击次数和最终时间 Public Class Form1 Dim Clicks As Integer = 0 'The variable that counts the number of times you clicked Dim Time As Integer 'The vairable tha

这是一个非常简单的代码。我刚刚开始使用vb。我在做一个游戏,你要用最少的时间和最少的点击量来解决一个难题。每次我调试程序时,它都会突出显示单击和最终时间,并表示未处理空引用异常。我在全球范围内宣布了点击次数和最终时间

Public Class Form1
Dim Clicks As Integer = 0  'The variable that counts the number of times you clicked
Dim Time As Integer 'The vairable that holds the time
Dim TimeMin As Integer 'The variable that holds the minutes
Dim TimeSec As Integer  'The variable that holds the seconds
Dim FinalTime As String 'The variable that holds the final time (minutes and seconds)
Dim NumArray() As Integer
Dim NumArray1() As String

Private Sub Times

    Time = Time + 1
    TimeSec = TimeSec + 1 
    TimeMin = Convert.ToInt32(TimeSec \ 60) 
    If Time >= 60 Then
        Time = 0
    End If
    FinalTime = TimeMin & " min " & Time & " seconds" 
    lblTime.Text = FinalTime 
End Sub
Private Sub Record(ByVal NumArray() As Integer, ByVal NumArray1() As String)

    For i As Integer = 0 To 1000 
        NumArray(i) = Clicks 'Problem is here
        i = +1
        Array.Sort(NumArray) 
    Next i
    lblRecordClicks.Text = NumArray(0) & " Clicks" 
    For k As Integer = 0 To 1000 
        NumArray1(k) = FinalTime 'Problem is here
        k = +1
        Array.Sort(NumArray1) 
    Next k
    lblRecordTime.Text = NumArray1(0) 
End Sub
规则1。VB.NET中的字符串与VB6中的字符串不同

在VB6中,您可以说

Dim text as String
您将初始化
text=”“

在VB.NET中,您需要

Dim text as String = vbNullString

这同样适用于字符串数组(或任何其他数组)


请参阅相关内容:

您在哪里声明
单击
最终时间
?如果不声明它们,则它们将为空。。也许尝试使用我也会建议打开Option Strict。您可以通过将其放在类文件的最顶端或在项目设置中全局执行来启用它们。请添加声明传递给此函数的数组的代码和调用此函数的代码。您在哪里声明传递给记录函数的两个数组?如何传递它们?看起来NumArray和NumArray1没有实例化,只是声明了,所以当您通过循环时,它会在NumArray(i)爆炸。
Dim text as New String
Dim arr as String() ' This makes arr = Nothing
arr = New String(10) { } ' This allocates an array with 11 items (0..10)
Dim arr as String() = New String(10) {}
Dim arr() As String = New String(10) {}
Dim arr() = New String(10) {}
Dim arr = New String(10) {}