Solved

Object reference not set to an instance of an object.

Posted on 2014-03-04
4
344 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

896 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

15 Experts available now in Live!

Get 1:1 Help Now