-1

This is my code, and when I run the project, there are no errors, but when it goes to my Dispense() event "An unhandled exception of type 'System.NullReferenceException' occurred" in the line ManDis = returnedMessage.Substring(8, 3)

It is necessary to do a try..catch or something?

Thanks!

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
JamieA
  • 1,984
  • 4
  • 27
  • 38

1 Answers1

0

Make sure that returnedMessage is actually set to a real string and is not null before you run the code. That is why it fails, because returnedMessage is null at the time of running that line.

The reason for that is that you're trying to call .Substring on something that is null, which is not possible.

By the way, welcome to Stackoverflow. A general suggestion is to only include the relevant code that causes the crashing, so you avoid wasting peoples time reading through irrelevant code. Also, don't forget to mention what you've tried so far, and what kind of research you did in the problem before you went here and asked for a solution.

Finally, if you like my answer, mark it as an answer and vote it up.

Mathias Lykkegaard Lorenzen
  • 15,031
  • 23
  • 100
  • 187
  • I dont have the returnedmessage set to a value in the Main sub and it fires off perfectly. I even removed the dispenser event and had all the information that would usually parse out in dispenser event firing off in the main. Of course it didnt parse out properly but it worked. so my question is why isnt it firing off in the same fashion in the dipenser event when it is set up to use the same public returnedmessage variable? – Kurtis Nestlé Snipes Henry Jul 22 '14 at 16:06