?
Solved

Object reference not set to an instance of an object.

Posted on 2014-03-04
4
Medium Priority
?
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
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 …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 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