0

I'm trying to ReDim an array of classes in this line here:

    For i As Integer = 0 To RacerList(i).CompatibilityArr.Count - 1
        For o As Integer = 0 To RacerList(i).CompatibilityArr.Count - 1
            ReDim Preserve RacerList(i).CompatibilityArr(o).HeightCompArr(AmountOfRacers - 1)
        Next
    Next

The Redim line of code results in "Object Reference not set to an instance of an object."

Above this line of code, I've got:

    For i As Integer = 0 To RacerList.Count - 1
        ReDim Preserve RacerList(i).CompatibilityArr(AmountOfRacers - 1)
    Next

Which works perfectly fine, so I'm pretty sure that the error is in HeightCompArr (also, when I write "HeightCompArr." it does not suggest the children inside of it in the drop down menu)

Below is all of the classes/structures/arrays that are on a separate module form:

Public RacerList As New List(Of Racer)

<Serializable()> Public Class Racer
    Public Property Name As String
    Public Property CleatSize As String
    Public Property SkillLevel As String
    Public Property Height As String
    Public Property Team As String
    Public CompatibilityArr() As Compatibility
End Class

<Serializable()> Public Class Compatibility
    Public HeightCompArr() As HeightComp
End Class

<Serializable()> Public Class HeightComp
    Public Name As String
    Public Score As Integer
End Class

I'm not sure if this is a problem with just how I am containing arrays inside of arrays or what. I am hugely thankful for any advice.

  • Possible duplicate of [Visual Basic - ReDim Preserve - Object reference not set to an instance of an object](http://stackoverflow.com/questions/39432460/visual-basic-redim-preserve-object-reference-not-set-to-an-instance-of-an-ob) – Ken White May 09 '17 at 00:06
  • I can't manage to implement the solution of the other question to work for my problem -- This may be due to incorrect implementation or that the solution is not applicable, I am not sure – Harry Allen May 09 '17 at 00:19
  • 1
    Ken's answer is most likely correct. You've set the size of "CompatibilityArr", but it contains no instances of "Compatibility" within it; thus the error when you try to access the "HeightCompArr" inside. You need to loop across each index of "CompatibilityArr" and assign an instance to it: `CompatibilityArr(i) = New Compatibility()` – Idle_Mind May 09 '17 at 00:51
  • Any reason you're not using a [**`List(Of T)`**](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx)? – Visual Vincent May 09 '17 at 05:16

1 Answers1

1

You have a syntactical bug and a design problem.

The syntactical problem is, as others have noted, that REDIM PRESERVE only makes space for the objects, it does not actually create the objects. To fix this you need to create a new object for each element of the array.

The design problem you have is that you are trying to compare every racer with every other racer, but you are storing the comparison inside the Racer class. The Racer references should be inside the Comparison, not the other way around.

I'd recommend you use List(Of ...) instead of arrays and restructure as follows:

Private Sub Main()
  Dim RacerList As New List(Of Racer)
  Dim CompatibilityArr As New List(Of Compatibility)

  Dim Adam As New Racer With {.Name = "Adam", .CleatSize = "10", .SkillLevel = "2", .Height = "180", .Team = "Blue"}
  Dim Bill As New Racer With {.Name = "Bill", .CleatSize = "11", .SkillLevel = "3", .Height = "185", .Team = "Blue"}
  Dim Charlie As New Racer With {.Name = "Charlie", .CleatSize = "12", .SkillLevel = "4", .Height = "190", .Team = "Red"}
  RacerList.Add(Adam)
  RacerList.Add(Bill)
  RacerList.Add(Charlie)
  CompatibilityArr.Add(New Compatibility With {.Racer1 = Adam, .Racer2 = Bill, .HeightCompScore = 5})
  CompatibilityArr.Add(New Compatibility With {.Racer1 = Adam, .Racer2 = Charlie, .HeightCompScore = 6})
  CompatibilityArr.Add(New Compatibility With {.Racer1 = Bill, .Racer2 = Charlie, .HeightCompScore = 7})
End Sub


<Serializable()> Public Class Racer
  Public Property Name As String
  Public Property CleatSize As String
  Public Property SkillLevel As String
  Public Property Height As String
  Public Property Team As String
End Class

<Serializable()> Public Class Compatibility
  Public Racer1 As Racer
  Public Racer2 As Racer
  Public HeightCompScore As Integer
End Class
SSS
  • 4,807
  • 1
  • 23
  • 44