0

I am trying to run a loop through an array of labels (called 'rank'), and make each label's text set to the value of a list of highscore's values.

Dim highScores As New List(Of Integer)
Dim rank() As Control = {Label1, Label2, Label3, Label4, Label5}

Private Sub High_Scores_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    highScores.Add(points)
    highScores.Sort()
    For index As Integer = 0 To highScores.Count()
        rank(index).Text = highScores(index)
    Next index
End Sub

When I run this, I get:

Object reference not set to an instance of an object

at line "rank(index).Text = highScores(index)". I have tweaked a bit off the stuff and I think I'm using the control array incorrectly, but I can't find a way to use it correctly.

Humayun Shabbir
  • 2,961
  • 4
  • 20
  • 33
iraex
  • 1
  • 1
  • Maybe not Dim rank() As Control but Dim rank() As Array? – Leo Chapiro Jul 30 '14 at 11:22
  • I get 'Text is not a member of 'System.Array' error for rank(index).Text). If I make Dim rank() As Control to Dim rank() As String, and then change Label1 etc. to Label1.Text (and of course change rank(index).Text to rank(index)), I get "Object reference not set to an instance of an object" as well. – iraex Jul 30 '14 at 11:53

2 Answers2

1

I bet the error occurs at the last iteration. You need to subtract 1 of Count.

For index As Integer = 0 To (highScores.Count() - 1)

Also, you need to be sure that the length of rank is greater than or equal to the length of highScores. If not, bad things will happen.

If (index < rank.Length) Then

Example

Private Sub High_Scores_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If (rank Is Nothing) Then Throw New Exception("Rank is null.")
    If (highScores Is Nothing) Then Throw New Exception("highScores is null.")
    highScores.Add(points)
    highScores.Sort()
    For index As Integer = 0 To (highScores.Count() - 1)
        If (index >= rank.Length) Then Throw New Exception("Rank to short.")
        If (rank(index) Is Nothing) Then Throw New Exception(String.Format("Rank element #{0} is null.", index))
        rank(index).Text = highScores(index)
    Next
    'If you hit any of the "null" exception see the following SO post:
    'http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it
End Sub
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
  • Unfortunately, the debugger (I use Visual Studio. I know the question got put into vb.net because I put in a tag called vb, though I'm not sure that matters) shows that it stops at the line after "For index..." (at index=0), so it doesn't even get to the last iteration. I did try your solution and the same error occurs. Length of rank is greater than highScores, which is 1, but it may exceed rank later on. So far it isn't the problem, but I'm not sure why highScores having a higher count would be a problem? – iraex Jul 30 '14 at 11:42
  • Instead of `rank() As Label = {...}`, I did `rank(4) As Label` and then set the values for each index below. I feel that this is less elegant, but it must've been the way that I did it earlier that returned me with null values. – iraex Jul 30 '14 at 12:33
0

As you want to show a maximum of rank.Length labels, you just need to find the smaller of rank.Length and highScores.Count():

Dim highScores As New List(Of Integer)
Dim rank() As Label = {Label1, Label2, Label3, Label4, Label5}

Private Sub High_Scores_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    highScores.Add(points)
    highScores.Sort()
    For index As Integer = 0 To Math.Min(highScores.Count(), rank.Length) - 1
        rank(index).Text = highScores(index).ToString()
    Next index
End Sub

You might as well declare rank() as being of type Label, and you appear to have forgotten to use .ToString() when showing the values.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84