Solved

Object reference not set to an instance of an object.

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

705 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

13 Experts available now in Live!

Get 1:1 Help Now