Solved

Object reference not set to an instance of an object.

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 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