引用未设置为实例错误

时间:2014-07-21 21:32:22

标签: vb.net nullreferenceexception

这是我的代码,当我运行项目时,没有错误,但是当它转到我的Dispense()事件时,“ManDis = returnedMessage”行中出现了“System.NullReferenceException类型未处理的异常”。子串(8,3)

有必要尝试一下吗?等等?

谢谢!

Module Module1

Public WithEvents ANDI As ANDI_AXC_LIBLib.ANDI_AXC
Public WithEvents ANDI_LOOP As ANDI_AXC_LIBLib.ANDI_AXC



Dim MAnufacturer As String
Public returnedMessage As String
Dim system As String
Dim version As String
Dim software As String
Dim connecttion As String
Dim logical As String
Dim DClass As String
Dim ID As String
Dim ServiceCount As String
Dim servicesupplied As String



' ANDI Library access 

Sub Main()

    ANDI = New ANDI_AXC


    'IP list for the console
    Dim addys As New List(Of String)

    addys.Add("10.0.99.139")
    addys.Add("10.0.99.138")
    addys.Add("10.0.99.181")


    For Each addy As String In addys



        With ANDI
            .DeviceClass = "POS01"
            .ServiceClass = "DGS"
            .Mode = ANDI_AXC_LIBLib._COMM_MODE.ETHERNET
            .CommPort = 1
            .EthernetPort = 6627
            .BaudRate = 9600
            .DataBits = 8
            .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY
            .StopBits = 2

        End With
        Dim MyResult As ANDI_AXC_LIBLib.CONN_OP_RESULT ' The return value from Open (found in the type library)


        ANDI.DeviceID = addy


        ANDI.Close()
        ' Are we already connected?
        If ANDI.Connected Then
            ' Close the connection.
            Call ANDI.Close()
        Else

        End If
        ' Open the connection.
        MyResult = ANDI.Open()
        MsgBox(MyResult.ToString())



        If MyResult = ANDI_AXC_LIBLib._CONN_OP_RESULT.CONN_OP_FAILED Then
            Console.WriteLine(ANDI.StatusMessage)
            Console.WriteLine("you are not connected")



        Else
            Console.WriteLine("Press enter to connect to the Allied network")

        End If




        SendMessage("DIS00 E00", 2)





        Console.ReadLine()
        Call ANDI.Open()




        Console.WriteLine("You are connected to the NexGen at: " & ANDI.DeviceID)
        MsgBox("You are connected!!!!")
        Console.WriteLine("")






        MAnufacturer = returnedMessage.Substring(9, 20)
        Console.WriteLine("Manufacturer = " + MAnufacturer)

        'Device Name
        system = returnedMessage.Substring(29, 15)
        Console.WriteLine("Device Name = " + system)

        'Version name
        version = returnedMessage.Substring(44, 5)
        Console.WriteLine("Version = " + version)

        'Software Identification
        software = returnedMessage.Substring(49, 20)
        Console.WriteLine("Software = " + software)

        'Connection Status
        connecttion = returnedMessage.Substring(69, 1)
        Console.WriteLine("Connection Status = " + connecttion)

        'Logical Status 
        logical = returnedMessage.Substring(70, 1)
        Console.WriteLine("Logical Status = " + logical)

        'Device Class  
        DClass = returnedMessage.Substring(71, 3)
        Console.WriteLine("Device Class = " + DClass)

        'ID
        ID = returnedMessage.Substring(74, 2)
        Console.WriteLine("ID = " + ID)

        ' number of services supplied 
        ServiceCount = returnedMessage.Substring(76, 2)
        Console.WriteLine("Number of Service = " + ServiceCount)

        'services Supplied
        servicesupplied = returnedMessage.Substring(78)




        Dim sb As New System.Text.StringBuilder
        For Each s As String In SplitIntoSets(servicesupplied, 3)
            sb.AppendLine(s)
        Next
        Console.WriteLine("Services Suppled ")
        Console.WriteLine("---------------------")
        Console.WriteLine(sb.ToString)

    Next
    Console.ReadLine()

End Sub
  Private Sub SendMessage(ByVal message As String, ByVal target As Integer)


    Dim MyResult As Boolean
    ' Are we connected?


    If Not ANDI.Connected Then Exit Sub ' Nope.


    Dim result As Boolean

    result = message.Contains("DIS")
    If result Then  <<'this is my DIS string to determine which event will be handled**
        dispenser()
    End If



    ' Did the user type anything?
    If message = "" Then Exit Sub ' Nope.





    ' Send the contents of the text box.
    MyResult = ANDI.SendMessage((message))



    'System.Threading.Thread.Sleep(1000)
    Console.WriteLine("result: " & MyResult)
    'MsgBox("my result: " & MyResult)
    ' How did we do?
    If MyResult Then
        If target = 1 Then
            Console.WriteLine("Message Sent 1")
        Else
            Console.WriteLine("Message Sent else")
        End If
    Else
        Beep()
        If target = 1 Then
            Console.WriteLine(ANDI.StatusMessage)
        Else
            Console.WriteLine(ANDI.StatusMessage)
        End If
    End If
End Sub

 Sub Dispenser()


    Dim ManDis As String
    Dim DisCount As String
    Dim DisNum As String
    Dim DisDetail As String

    ManDis = Nothing
    DisCount = Nothing
    DisNum = Nothing
    DisDetail = Nothing


    ' ANDI Library access 
    ANDI = New ANDI_AXC


    'IP list for the console
    Dim addys As New List(Of String)

    addys.Add("10.0.99.139")
    addys.Add("10.0.99.138")
    addys.Add("10.0.99.181")


    For Each addy As String In addys



        With ANDI
            .DeviceClass = "POS01"
            .ServiceClass = "DGS"

            .CommPort = 1
            .EthernetPort = 6627
            .BaudRate = 9600
            .DataBits = 8
            .Parity = ANDI_AXC_LIBLib._PARITY_MODE.NO_PARITY
            .StopBits = 2

        End With
        Dim MyResult As ANDI_AXC_LIBLib.CONN_OP_RESULT ' The return value from Open (found in the type library)


        ANDI.DeviceID = addy


        ANDI.Close()
        ' Are we already connected?
        If ANDI.Connected Then

        Else

        End If
        ' Open the connection.
        MyResult = ANDI.Open()
        MsgBox(MyResult.ToString())


        Console.ReadLine()
        Call ANDI.Open()




        Console.WriteLine("You are connected to the NexGen at: " & ANDI.DeviceID)
        MsgBox("You are connected!!!!")
        Console.WriteLine("")



        ManDis = returnedMessage.Substring(8, 3)<<<<<<<<<<<<<<< my error happens here.
        Console.WriteLine("Manufacturer = " + ManDis)

        'Device Name
        DisCount = returnedMessage.Substring(29, 2)
        Console.WriteLine("Device Name = " + DisCount)

        DisNum = returnedMessage.Substring(100, 2)
        Console.WriteLine("Device Name = " + DisNum)


        DisDetail = returnedMessage.Substring(29, 6)
        Console.WriteLine("Device Name = " + DisDetail)


        End



    Next
    Console.ReadLine()



End Sub

1 个答案:

答案 0 :(得分:0)

在运行代码之前,请确保returnedMessage实际设置为实际字符串且不是null。这就是它失败的原因,因为returnedMessage在运行该行时为null

原因是您尝试在.Substring上调用null,这是不可能的。

顺便说一句,欢迎来到Stackoverflow。一般建议是仅包含导致崩溃的相关代码,这样可以避免浪费人们阅读不相关代码的时间。此外,不要忘记提到你到目前为止所尝试的内容,以及在你来到这里并要求解决方案之前你在这个问题上做了哪些研究。

最后,如果您喜欢我的回答,请将其标记为答案并进行投票。