1

I've got a 2D array of 100 labels in a 10x10 matrix (it's 2D because it represents some hardware out in the real world, if anyone cares). I want to loop through and check a conditional, and change the label background color if the conditional is false.

I've tried this ten different ways, but I keep getting an exception thrown because the temp variable I have created won't take an assignment to one of the label names.

'Table for correct switch module for corresponding actuator
Dim ActLabelLookup(,) As Label =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}

Private Sub UpdateActuatorStatus()
Dim X As Integer
Dim Y As Integer
Dim CurrAct As New Label

For X = 0 To (ActControl.MAX_X - 1)
    For Y = 0 To (ActControl.MAX_Y - 1)
        If TempFunctionalActuatorMatrix(X, Y) = False Then
            CurrAct = ActLabelLookup(X, Y)
            CurrAct.BackColor = Color.Firebrick
        End If
    Next
Next

End Sub

With this code, CurrAct is never getting set to anything. Anyone see what I'm doing wrong?

Screen Cap

7
  • Check the value of ActControl.MAX_X - 1 and ActControl.MAX_Y - 1 to ensure that it does not exceed ActLabelLookup.GetUpperBound(0) and ActLabelLookup.GetUpperBound(1), respectively. What is the actual error message. Commented Nov 5, 2015 at 18:15
  • What is the exception? Commented Nov 5, 2015 at 18:17
  • .Net are zero-based index, that is a 9x9 matrix. Dim ActLabelLookup(,) As New Label(9, 9) {...} Commented Nov 5, 2015 at 18:18
  • The exception is thrown the first time my conditional resolves to False (in this case, when X=0 and Y=1). So I don't believe my issue is with exceeding upper bounds. The actual error is "An unhandled exception of type 'System.NullReferenceException' occurred in FW Qualification Suite.exe". It specifically flags the CurrAct.BackColor = Colr.Firebrick line, which is assume is because CurrAct = Nothing when I view it in the Watch window of VS15. Commented Nov 5, 2015 at 18:30
  • Does this code run after the form with MTA1, MTA2, ... MTA100 is loaded so that the label are initialized? Commented Nov 5, 2015 at 18:58

2 Answers 2

1

Your array isn't initialised (well, it is, but it is initialised with nothings as the labels are nothing as the form instance is created).

Try filling it before parsing (in Form Load or in UpdateActuatorStatus):

     ActLabelLookup =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}
Sign up to request clarification or add additional context in comments.

2 Comments

@Mort Ah, of course, the FormLoad is the event that triggers that label creation, and my initialization occurs before that. Thanks!
@nobby Form_Load might trigger your label creation, but it's far more likely this is happening in the InitializeComponent() method.
0

Change the member-level declaration of ActLabelLookup to just:

Dim ActLabelLookup(,) As Label

In the form's Load event handler add a line to initialize it:

ActLabelLookup(,) =
{{MTA91, MTA92, MTA93, MTA94, MTA95, MTA96, MTA97, MTA98, MTA99, MTA100},
{MTA81, MTA82, MTA83, MTA84, MTA85, MTA86, MTA87, MTA88, MTA89, MTA90},
{MTA71, MTA72, MTA73, MTA74, MTA75, MTA76, MTA77, MTA78, MTA79, MTA80},
{MTA61, MTA62, MTA63, MTA64, MTA65, MTA66, MTA67, MTA68, MTA69, MTA70},
{MTA51, MTA52, MTA53, MTA54, MTA55, MTA56, MTA57, MTA58, MTA59, MTA60},
{MTA41, MTA42, MTA43, MTA44, MTA45, MTA46, MTA47, MTA48, MTA49, MTA50},
{MTA31, MTA32, MTA33, MTA34, MTA35, MTA36, MTA37, MTA38, MTA39, MTA40},
{MTA21, MTA22, MTA23, MTA24, MTA25, MTA26, MTA27, MTA28, MTA29, MTA30},
{MTA11, MTA12, MTA13, MTA14, MTA15, MTA16, MTA17, MTA18, MTA19, MTA20},
{MTA1, MTA2, MTA3, MTA4, MTA5, MTA6, MTA7, MTA8, MTA9, MTA10}}

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.