0

http://pastebin.com/3A4P61Gt The code in question is specifically at line 143. Whenever I try to access a label in the array like so Dicelbls(0).Text I get a null reference error. Obviously I am not declaring the array right, any suggestions?

2

4 Answers 4

1

You are right, the problem is at line 143:

Dim Dicelbls As Label() = {lblP1Die0, lblP1Die1, lblP1Die2, lblP2Die0, lblP2Die1, lblP2Die2, lblP1Score, lblP2Score}

Specifically, at the point in the object initialization process when this code runs, the references behind those Label variables are still null/Nothing. So you're putting references to Nothing into your array.

To fix the code, move the initialization to the Form_Load event instead.

Sign up to request clarification or add additional context in comments.

Comments

0

Try to add initialization in Form_Load event.

Dim Dicelbls As Label()

Private Sub Form1_Load(..)
  Dicelbls= new Label() {lblP1Die0, lblP1Die1, lblP1Die2, lblP2Die0, lblP2Die1, lblP2Die2, lblP1Score, lblP2Score}
....
End Sub

Comments

0

You're declaring the array correctly, but in the wrong place. Leave the variable declaration where it is, and move the assignment to somewhere after the form is created.

Class frmMain

    Dim Dicelbls As Label()

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dicelbls = {lblP1Die0, lblP1Die1, lblP1Die2, lblP2Die0, lblP2Die1, lblP2Die2, lblP1Score, lblP2Score}
    End Sub

    ...

End Class

Comments

0

Try this one:

Dim Dicelbls(8) As Label
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dicelbls = {lblP1Die0, lblP1Die1, lblP1Die2, lblP2Die0, lblP2Die1, lblP2Die2, lblP1Score, lblP2Score}
End Sub

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.