Error  Object reference not set to an instance of an object

Posted on 2009-04-28
Last Modified: 2012-05-06
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

Question by:cmdolcet
    LVL 18

    Accepted 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
    LVL 26

    Expert Comment

    by:Anurag Thakur
    try this
    Me.Controls("lblreadings" & position.ToString()).Text = indvalue
    LVL 62

    Expert Comment

    by:Fernando Soto
    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

    LVL 62

    Expert Comment

    by:Fernando Soto
    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?


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Title # Comments Views Activity
    Record open by another user 6 23
    Handling arrays 14 27
    GridDataView 6 3
    Merging PDFs with VB.NET 10 0
    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    This video discusses moving either the default database or any database to a new volume.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now