首页 > 解决方案 > VB:.Net Core - 尽管定义了结构并初始化了结构,但在使用结构时获取“未设置对象引用”

问题描述

我似乎遇到了在命令行 Visual Basic .NET (core 5) 项目中使用结构的问题。在其中我有一个处理命令队列/缓冲系统的类。

这是类代码:

Public Class ClassCommandBuffer

' CmdMax is the maximum number of commands allowed within a set (0 to CmdMax -1)
Public Const MaxCmdSets As Integer = 30
Public Const MaxNoofCmdLines As Integer = 30

' CurrentNestIndex contains the current nest level. If this is set to -1 then buffer comamnds aren't being run
' NestedMaxIndex is the maximum number of nested buffer command sets allowed (0 to NextedMax -1)
' CmdMax is the maximum number of commands allowed within a set (0 to CmdMax -1)

Public Structure CmdBufferCmdStruct
    Public CmdLines() As String     ' Array of Command Lines within current set
    Public NoofCmds As Integer      ' Number of Command Lines
    Public CurrentIndex As Integer  ' Usually set to -1, but if cmdset being run then reflects the current cmd index
End Structure

Public Structure CmdBufferLoadingStruct
    Public Index As Integer         ' When loading a command set in, this will be set to the new command set index, otherwise it's -1
    Public CmdIndex As Integer      ' When loading a commadn set in, this is the command index of the next command to be loaded, otherwise -1
End Structure

Public Structure CmdBufferStruct
    Public Loading As CmdBufferLoadingStruct        ' Used when loading a new command set
    Public CmdSet() As CmdBufferCmdStruct           ' Array of command sets 
    Public CurrentNestIndex As Integer              ' Current nested index being processed
End Structure

Public CmdBuffer As CmdBufferStruct




' --- Initialise values & setup ---
Public Sub New()
    ReDim CmdBuffer.CmdSet(MaxCmdSets)                              ' declates 30 sets
    ReDim CmdBuffer.CmdSet(MaxCmdSets).CmdLines(MaxNoofCmdLines)    ' declates 30 sets of 30 cmds each
    CmdBuffer.CurrentNestIndex = -1                                 ' initialise the current nest index to -1 (buffer cmds not running)
    CmdBuffer.Loading.Index = -1
    CmdBuffer.Loading.CmdIndex = 0
    Console.WriteLine("> New")
End Sub

' --- Command Sets ---
Public Sub NewCmdSetStart()
    Console.WriteLine("> NewCmdSetStart")
    With CmdBuffer
        .Loading.Index = .CurrentNestIndex + 1
        .Loading.CmdIndex = 0
    End With
End Sub

Public Sub NewCmdSetEnd()
    Console.WriteLine("> NewCmdSetEnd")
    With CmdBuffer
        .CmdSet(.Loading.Index).NoofCmds = .Loading.CmdIndex
        .Loading.Index = -1
        .CurrentNestIndex = .CurrentNestIndex + 1
    End With
End Sub

Public Sub NewCmdSetInsert(newcmd As String)
    Console.WriteLine("> NewCmdSetInsert : " & newcmd)
    With CmdBuffer
        .CmdSet(.Loading.Index).CmdLines(.Loading.CmdIndex) = newcmd
        Dim x As Integer = .Loading.CmdIndex + 1
        .Loading.CmdIndex = x
    End With
End Sub

' --- Command processing ---
Public Function GetNextCmd() As String
    ' check to see if we've hit the end cmds or not running from buffer
    If CmdBuffer.CurrentNestIndex = -1 Then
        Console.WriteLine("Error: We're not running buffer commands! Why was GetNextCmd called? Line " & CMDlineCount)
        EndAssembler()
    End If

    Dim CurCmdLine As Integer = CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).CurrentIndex
    Dim CurCmdNoof As Integer = CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).NoofCmds

    If CurCmdLine >= CurCmdNoof Then
        Console.WriteLine("Error: We've reach the end of cmds! Why was GetNextCmd called? Should check before calling. Line " & CMDlineCount)
        EndAssembler()
    End If

    Return CmdBuffer.CmdSet(CmdBuffer.CurrentNestIndex).CmdLines(CurCmdLine)
End Function

Public Function IsBufferRunning() As Boolean
    If CmdBuffer.CurrentNestIndex = -1 Then Return False Else Return True
End Function

结束类

该类在 MainModule 中定义,因此:

Module ModuleMain

Public CommandBuffer As New ClassCommandBuffer

Sub Main(args As String())

调用代码如下所示:

        With ModuleMain.CommandBuffer
            .NewCmdSetStart()
            .NewCmdSetInsert(ifCmd)
            .NewCmdSetEnd()
        End With

IfCmd 是一个字符串变量,其中包含要存储的命令。

我遇到的问题是,当调用“ NewCmdSetInsert(ifCmd) ”时,它在函数内的行上崩溃:“ Dim x As Integer = .Loading.CmdIndex + 1 ”并出现以下错误:“ System. NullReferenceException:“对象引用未设置为对象的实例。” '原来的说法是

            .Loading.CmdIndex = .Loading.CmdIndex + 1

但是我用临时的 Dim x... 行将其分开,以尝试查看问题出在哪里。

似乎是说我没有将结构定义为新的,但我在 MainModule 中有。此外,在类的“新建”部分中,我为 .Loading.CmdIndex 设置了一个初始值,我不太明白问题是什么,因为我得到以下输出,表明“新建”首先运行(因此初始化数组和值):此时它因上述错误而中断并突出显示(忽略“BCC 20480”,因为这是传递给 NewCmdSetInsert 的命令字符串)

新的

NewCmdSetStart

新命令集插入:密件抄送 20480

有人有什么想法吗?

标签: vb.net

解决方案


推荐阅读