none
VB.NET ArrayList Structure NullReference Exception

    Question

  • I can not figure this out. I keep Getting a NullReferenceException Error but I have declared a NEW structure. See below:

        Structure ISACReply
            Public RTD As ArrayList
            Public SAC As ArrayList
            Public DRT As ArrayList
        End Structure

        Sub calltest()
            Dim reply As New ISACReply()
            reply = test()
            MsgBox(reply.DRT.Item(0).tostring)
        End Sub

        Function test() As ISACReply
            Dim rv As New ISACReply
            rv.DRT.Add("HELLO")
            rv.SAC.Add("THERE")
            rv.DRT.Add("WORLD")
            Return rv
        End Function

    When I try to execute the subroutine "calltest" I get a NullReferenceException in the line that says
    " rv.DRT.Add("HELLO") "  even though I have set rv to an instance of ISACReply using NEW. Please help.

    Saturday, September 26, 2009 11:48 PM

Answers

  • You should consider to redo the structure again because there is no way you can create instance of ArrayList without the keyword "New" and you cannot declare structure member with New.
    Try this sample tutorial, maybe it will help http://enerjy.sys-con.com/node/204816

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Marked as answer by john.m.graham Sunday, September 27, 2009 12:27 AM
    Sunday, September 27, 2009 12:17 AM
  • You could have done it like this too


        Structure ISACReply
            Public RTD As ArrayList
            Public SAC As ArrayList
            Public DRT As ArrayList
        End Structure
    
        Sub calltest()
            Dim reply As New ISACReply()
            reply = test()
            MsgBox(reply.DRT.Item(0).ToString)
        End Sub
    
        Function test() As ISACReply
    
            Dim rv As New ISACReply
    
            rv.DRT = New ArrayList
            rv.SAC = New ArrayList
    
            rv.DRT.Add("HELLO")
            rv.SAC.Add("THERE")
            rv.DRT.Add("WORLD")
            Return rv
    
        End Function

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    • Marked as answer by john.m.graham Sunday, September 27, 2009 12:26 AM
    Sunday, September 27, 2009 12:19 AM

All replies

  • Always use keyword "New" 
    Structure ISACReply
            Public RTD As New ArrayList
            Public SAC As New ArrayList
            Public DRT As New ArrayList
        End Structure

    kaymaf


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Sunday, September 27, 2009 12:00 AM
  • Thanks for the answer but now I'm getting
    "Non-shared members in a Structure cannot be declared 'New'
    when I make the changes you describe.
    Sunday, September 27, 2009 12:06 AM
  • You should consider to redo the structure again because there is no way you can create instance of ArrayList without the keyword "New" and you cannot declare structure member with New.
    Try this sample tutorial, maybe it will help http://enerjy.sys-con.com/node/204816

    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Marked as answer by john.m.graham Sunday, September 27, 2009 12:27 AM
    Sunday, September 27, 2009 12:17 AM
  • Here's my workaround:

        Structure ISACReply
            Shared RTD As New ArrayList
            Shared SAC As New ArrayList
            Shared DRT As New ArrayList
            Public junk As String
        End Structure

    I had to add junk field because without it I get "Structure must contain as least one instance member variable"

    If there's an easier way please let me know.

    Thanks kaymaf
    Sunday, September 27, 2009 12:17 AM
  • You could have done it like this too


        Structure ISACReply
            Public RTD As ArrayList
            Public SAC As ArrayList
            Public DRT As ArrayList
        End Structure
    
        Sub calltest()
            Dim reply As New ISACReply()
            reply = test()
            MsgBox(reply.DRT.Item(0).ToString)
        End Sub
    
        Function test() As ISACReply
    
            Dim rv As New ISACReply
    
            rv.DRT = New ArrayList
            rv.SAC = New ArrayList
    
            rv.DRT.Add("HELLO")
            rv.SAC.Add("THERE")
            rv.DRT.Add("WORLD")
            Return rv
    
        End Function

    coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read .
    • Marked as answer by john.m.graham Sunday, September 27, 2009 12:26 AM
    Sunday, September 27, 2009 12:19 AM
  • Thank You.
    Sunday, September 27, 2009 12:35 AM
  • Yes this works fine but if you don't want to think about creating those ArrayLists in another routine, you can also create it as a class instead of a structure.

    For example:

        Class ISACReply
            Public RTD As New ArrayList
            Public SAC As New ArrayList
            Public DRT As New ArrayList
        End Class
    
        Sub calltest()
            Dim reply As New ISACReply
            reply = test()
            MsgBox(reply.DRT.Item(0).ToString)
        End Sub
    
        Function test() As ISACReply
            Dim rv As New ISACReply
            rv.DRT.Add("HELLO")
            rv.SAC.Add("THERE")
            rv.DRT.Add("WORLD")
    
            Return rv
        End Function
    

    I found that this works quite nicely for me. (At least so far, I'm not positive about efficiency yet...)

    [ Hopefully this helps someone (even though this is a very delayed reply). ]

    Thursday, October 31, 2013 1:12 AM