Solved

Object reference not set to an instance of an object.

Posted on 2014-03-04
4
350 Views
Last Modified: 2014-03-04
Why is Me.Controls("d" & currentlabel).Text = x saying that "Object reference not set to an instance of an object" How do i fix it?

            Dim currentmonth As Integer = DatePart(DateInterval.Month, dtpselector.Value)
            Dim currentyear As Integer = DatePart(DateInterval.Year, dtpselector.Value)
            Dim previousmonth As Integer = DatePart(DateInterval.Month, DateAdd(DateInterval.Month, -1, dtpselector.Value))
            Dim previousyear As Integer = DatePart(DateInterval.Year, DateAdd(DateInterval.Month, -1, dtpselector.Value))
            Dim nextmonth As Integer = DatePart(DateInterval.Month, DateAdd(DateInterval.Month, 1, dtpselector.Value))
            Dim nextyear As Integer = DatePart(DateInterval.Year, DateAdd(DateInterval.Month, 1, dtpselector.Value))
            Dim blankstofill As Integer = DatePart(DateInterval.Weekday, dtpselector.Value) - 1
            Dim currentenddate As Integer = Date.DaysInMonth(currentyear, currentmonth)
            Dim previousenddate As Integer = Date.DaysInMonth(previousyear, previousmonth)
            Dim currentlabel As Integer = 1
            'fill previous month data
            For x As Integer = blankstofill - previousenddate To previousenddate
                Me.Controls("d" & currentlabel).Text = x
                currentlabel += 1
            Next
            'fill current month data with no previous month data
            For x As Integer = 1 To currentenddate
                Me.Controls("d" & currentlabel).Text = x
                currentlabel += 1
            Next
            ''fill next month data
            For x As Integer = 1 To 42 - blankstofill - currentenddate
                Me.Controls("d" & currentlabel).Text = x
                currentlabel += 1
            Next

Open in new window

0
Comment
Question by:Millkind
  • 2
4 Comments
 
LVL 22

Expert Comment

by:rspahitz
ID: 39903896
Do you have a label named "d1"?
0
 

Author Comment

by:Millkind
ID: 39903906
Yes
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39904026
There are only two possibilities:
(1) You've got the name wrong...you've ruled that out.
(2) You've got the wrong container.

Your code is:

    Me.Controls("d" & currentlabel)

The "Me" refers to the Form itself.  If the control is not directly contained by the form, then it will not be found.  For instance, that particular control might be in a Panel or a GroupBox.

To fix the problem, either:
(1) Replace "Me" with the proper container name, such as "Panel1" or "GroupBox1":

    Panel1.Controls("d" & currentlabel)

(2) Search for the control using code like below.  This will find the control where ever it is:
Dim matches() As Control
matches = Me.Controls.Find("d" & currentlabel, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is Label Then
    Dim lbl As Label = DirectCast(matches(0), Label)
    lbl.Text = x
End If

Open in new window

0
 

Author Closing Comment

by:Millkind
ID: 39904223
Wonderful explanation.  I was using a tabcontrol and needed to change me to the tap page name.  Thanks so much.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

777 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