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.

2
  • Maybe not Dim rank() As Control but Dim rank() As Array? Commented Jul 30, 2014 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. Commented Jul 30, 2014 at 11:53

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.