none
Trouble with object references RRS feed

  • Question

  • I'm trying to migrate a large app from vb6 to vb.net 2008 and beyond. To simplify the process I am starting with a subset of adapted code. In this example, I have created an object Mushroom with an associated collection Locations. Here is a code snippet in which I am trying to add a location object to the collection but get "object reference not set to instance of an object" in the statement encased in the try-catch trap. Thanks for any help.

    Option Explicit On
    Friend Class cLocations

    'Initialize new collection
        Dim mLocations As New Collection

      Public Function Add(ByRef cRecordID As Integer, ByRef cMushroomID As Integer, ByRef cDescription As String, ByRef cLogDate As Date, ByRef cLatitude As String, ByRef cLongitude As String) As Object

            Dim NewLocation As New cLocation      'cLocation external object class
            With NewLocation
                .ID = NextID()
                .RecordID = cRecordID
                .MushroomID = cMushroomID
                .Description = cDescription
                .LogDate = cLogDate
                .Latitude = cLatitude
                .Longitude = cLongitude
            End With

            Try
                mLocations.Add(NewLocation, CStr(NewLocation.ID))     '*** error here with mLocations ***
            Catch ex As Exception
                MsgBox(ex.Message())
            End Try

            Add = NewLocation
        End Function

    End Class

    Thursday, November 14, 2019 4:51 PM

All replies

  • Hello,

    Set a breakpoint and when hit hover over  Dim NewLocation and inspect the properties which should give you insight to the issue.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, November 14, 2019 5:04 PM
    Moderator
  • Hi

    I think your problem stems from cLocation being declared as Collection, but no indication of type.

    Here is your code, changed to make it workable as a stand alone. I have used a Dictionary (Typed as Integer, cLocation) to hold the ID and cLocation to replace your Collection. (Typed as Integer, cLocation)

    I have also changed the code flow slightly, as you can see.

    ' Form1 with a Multiline TextBox1
    Option Strict Off
    Option Explicit On
    Public Class Form1
    	' crate a Dictionary of
    	Dim mLocations As New Dictionary(Of Integer, cLocation)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' here,add a series of new 
    		' cLocation to a Dictionary
    		' of cLocation and ID
    		' these items are just using
    		' some random contents, but 
    		' would likely be from some 
    		' other source
    		For i As Integer = 1001 To 1021
    			Dim nc As cLocation = Add(i, i \ 3, "cDescription" & i.ToString, Now.AddDays(i \ 211), "123.23." & (i Mod 360).ToString, "300.33." & (i Mod 360).ToString)
    			mLocations.Add(nc.ID, nc)
    		Next
    
    		' just a random choice to show
    		' retrieval
    		With mLocations(5)
    			TextBox1.AppendText(.ID.ToString & vbCrLf)
    			TextBox1.AppendText(.RecordID.ToString & vbCrLf)
    			TextBox1.AppendText(.MushroomID.ToString & vbCrLf)
    			TextBox1.AppendText(.Description & vbCrLf)
    			TextBox1.AppendText(.LogDate.ToString("ddd, dd MMM yyyy") & vbCrLf)
    			TextBox1.AppendText(.Latitude & vbCrLf)
    			TextBox1.AppendText(.Longitude & vbCrLf)
    		End With
    
    	End Sub
    
    	' here, the function creates and 
    	' returns a new cLocation
    	Public Function Add(cRecordID As Integer, cMushroomID As Integer, cDescription As String, cLogDate As Date, cLatitude As String, cLongitude As String) As cLocation
    		' must be unique for use as
    		' Dictionary key
    		Static nextid As Integer = 1
    		Dim NewLocation As New cLocation
    		With NewLocation
    			.ID = nextid
    			.RecordID = cRecordID
    			.MushroomID = cMushroomID
    			.Description = cDescription
    			.LogDate = cLogDate
    			.Latitude = cLatitude
    			.Longitude = cLongitude
    		End With
    		nextid += 1
    		Return NewLocation
    	End Function
    
    	Class cLocation
    		Property ID As Integer
    		Property RecordID As Integer
    		Property MushroomID As Integer
    		Property Description As String
    		Property cDescription As String
    		Property LogDate As DateTime
    		Property Latitude As String
    		Property Longitude As String
    	End Class
    End Class


    Regards Les, Livingston, Scotland

    Thursday, November 14, 2019 5:54 PM
  • When I break and hover over mLocations.Add(...), it doesn't show a data type for the collection - because it was never assigned as Les suggested. VB.net 2008 doesn't like me trying to cast the collection as type cLocation, or any data type, saying "Microsoft.VisualBasic.Collection has no type parameters and so cannot have type arguments". Says the same thing about Dictionary lists as well. So why does it complain "Object reference not set to instance of an object" if it doesn't let me assign the data type of my collection? There's got to be a way around this....
    Monday, November 18, 2019 7:18 PM
  • Hi

    The best I can do is to point you to HERE where maybe it will explain better than I can.

    I would suggest you consider one of the many other Collection types that are much more capable and offer lots of methods to ease their use.


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, November 18, 2019 8:20 PM
    Monday, November 18, 2019 8:13 PM
  • Never use the VisualBasic.Collection anymore. It was created in 2002 and has only problems. For the rest has Les written enough around this to solve your problem. 

    Success
    Cor

    Monday, November 18, 2019 11:43 PM
  • So I took your suggestion to heart to abandon VisualBasic.Collection and instead try Collections.Generic List (T) because it allows me to specify a new List (of cLocation) objects, as in this example, and has the various methods that carry over well from the old VB.Collection object for my needs. I should be able to substitute it in lieu of the old vb.collection with minimal code modifications. It didn't solve my problem however, still gives the same error "Object reference not set to instance of an object". There's got to be a simple solution to this, I just don't know what it is - and I'm stuck. TIA...

    Option Strict Off
    Option Explicit On
    Imports System.Collections.Generic

    Friend Class cLocations

        'Initialize local collection
        Dim mLocations As New List(Of cLocation)

        Public Function Add(ByRef cRecordID As Integer, ByRef cMushroomID As Integer, ByRef cDescription As              String, ByRef cLogDate As Date, ByRef cLatitude As String, ByRef cLongitude As String) As cLocation

          Dim NewLocation As New cLocation

          With NewLocation
                .ID = NextID()
                .RecordID = cRecordID
                .MushroomID = cMushroomID
                .Description = cDescription
                .LogDate = cLogDate
                .Latitude = cLatitude
                .Longitude = cLongitude
            End With

            mLocations.Add(NewLocation)     'error occurs here

            Add = NewLocation
        End FunctionEnd Class

    Friday, November 22, 2019 7:28 PM
  • Hi

    First of all. Please use the code block tool from the editor toolbar to post code.

    Here is yet another simple example - what is not to like about this simplicity?

    Option Strict Off
    Option Explicit On
    Public Class Form1
    	Dim mLocations As New List(Of cLocation)
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		' add one cLocation
    		mLocations.Add(New cLocation With {
    			.ID = 1,
    			.RecordID = 1234,
    			.MushroomID = 34567,
    			.Description = "Well well, some text!",
    			.LogDate = Now.AddDays(-2),
    			.Latitude = "306.54.32",
    		.Longitude = "77.12.23"})
    
    		' add another cLocation
    		mLocations.Add(New cLocation With {
    			.ID = 2,
    			.RecordID = 5432,
    			.MushroomID = 54376,
    			.Description = "Well well, some more text!",
    			.LogDate = Now.AddDays(-6),
    			.Latitude = "111.46.12",
    		.Longitude = "23.45.11"})
    
    	End Sub
    	Class cLocation
    		Property ID As Integer
    		Property RecordID As Integer
    		Property MushroomID As Integer
    		Property Description As String
    		Property cDescription As String
    		Property LogDate As DateTime
    		Property Latitude As String
    		Property Longitude As String
    	End Class
    End Class


    Regards Les, Livingston, Scotland


    Friday, November 22, 2019 8:12 PM
  • Hi,

    Did you resolve the issue? If you have resolved the issue then pleasse mark the post or posts that helped you as The Answer using the Mark as Answer link at the bottom of the post. Marking answers will help other community members find solutions in the future.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, November 25, 2019 3:15 AM
    Moderator
  • I love the simple elegance of Les' "yet another simple example" but it won't even pass syntax muster in my DE. I continue to struggle with this and will just have to figure it out on my own. Thanks for all your suggestions...
    Monday, November 25, 2019 8:12 PM
  • I love the simple elegance of Les' "yet another simple example" but it won't even pass syntax muster in my DE. I continue to struggle with this and will just have to figure it out on my own. Thanks for all your suggestions...

    Hi

    Well, since this is the VB.NET forum, and the code I posted was VB.NET code, and the code does compile and run correctly, then the only remaining dubious component is the OP!

    Good luck getting it to work on your own then.


    Regards Les, Livingston, Scotland


    • Edited by leshay Monday, November 25, 2019 9:11 PM
    Monday, November 25, 2019 9:10 PM