• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Error Object reference not set to an instance of an object

I get an object reference not set to an instance of an object with this line of code.   Me.Controls("lblreadings" & position).Text = indvalue
Dim position As Integer
        TmrIndicatorBitValues.Enabled = False
        For COMIndex = 0 To CurrCOMPort.Count - 1
            IndicatorPort = CurrCOMPort.Item(COMIndex)
            position = 0 'COMIndex + 1
            Select Case Position
                Case 0
                    Me.Controls("lblreadings" & position).Text = indvalue

Open in new window

  • 2
1 Solution
This is because it is assuming position to be an object. If position is a string then you have to write the following code:
Me.Controls("lblreadings" & "position").Text = indvalue
Anurag ThakurCommented:
try this
Me.Controls("lblreadings" & position.ToString()).Text = indvalue
Fernando SotoCommented:
Hi cmdolcet;

If you have numbered your controls with leading zeros then you will need to add those zeros to the name. For example if the control name is lblreadings01 then the statement should be as follows

Me.Controls("lblreadings" & position.ToString("00")).Text = indvalue

meaning that the controls is made up with two digits at the end with leading zeros as placeholders. If the controls have three decimal position on the end then us this:

Me.Controls("lblreadings" & position.ToString("000")).Text = indvalue

Fernando SotoCommented:
Hi cmdolcet;

So are you saying that the name of the control is lblreadingsposition and that the variable in your program position which is an Integer is not part of the name? Meaning the following:

Control Name is lblreadingsposition
Or is the control name lblreadingsXX where XX is numeric digits?


Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now