reference not set to an instance of an object. Please help!

12-25-07, 10:31 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
reference not set to an instance of an object. Please help!
Hi, my program compiles ok but when I click on a the control to execute this:
Public Sub numberUpdate(ByVal theNum As Integer)
lblCurrentQuestionNumber.Text = theNum
End Sub
I get this error message: System.NullReferenceException: Object reference not set to an instance of an object.
Can anyone help me with this please?
|

12-26-07, 09:58 AM
|
 |
Coding Addict
|
|
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
|
|
It's because 'lblCurrentQuestionNumber' cannot be found. Basically, you want to use find control, and link that to an instance of a text box:
asp Code:
Dim tb As TextBox = <parent control>.FindControl("lblCurrentQuestionNumber") tb.Text = theNum
However, depending on the control that you click to call this function, it probably won't have the same parameter declaration. Most likely, it will have a 'sender' and a set of event arguments. The sender would be the button or link button (etc) that was clicked. You can then pass 'theNum' as a command argument and retrieve it through the eventArg's parameter collection. Above, where '<parent control>', you need to use the 'sender' parameter and cast that to the correct type via CType. Then you can use findcontrol off of that.
If you post more of your surrounding code, may be of better help.
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
|

12-26-07, 10:28 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Here is what my code looks like that I'm using to call:
Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
Dim x As _default
x = New _default
Dim intSentNum As Integer
intSentNum = 1
x.numberUpdate(intSentNum)
End sub
Here's the sub that I call:
Public Sub numberUpdate(ByVal inComingNum As Integer)
lblCurrentQuestionNumber.Text = inComingNum
End Sub
|

12-26-07, 10:34 AM
|
 |
Coding Addict
|
|
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
|
|
First, you should convert to string before you assign as text value.
Where is 'lblCurrentQuestionNumber'? Is it wrapped in anything like a repeater, or detailsView, etc...?
If not, you can use:
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
|

12-26-07, 10:49 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I’ve tried using tostring() also my control is on the default page. After typing name of the control, 'lblCurrentQuestionNumber', and a period(.), I get a list of all the properties associated with the control. Does this means that I may still have a problem with my program finding the control?
|

12-26-07, 10:55 AM
|
 |
Coding Addict
|
|
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
|
|
What do you mean 'default page'... is it on a different page than where the function call is?
What happens when you run the debugger?
Are you attaching the control dynamically?
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
|

12-26-07, 11:12 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The default page is my home page where I have all my dynamic controls and user controls. I am calling the function from one of my user controls that I have on the same page as the dynamic control.
The program seems to work fine until I set a value to the text property of the control. When I run the program this is what I get:
Line 256: lblCurrentQuestionNumber.Text = "1".ToString()
I can access lblCurrentQuestionNumber with other controls like a command button that is on the same page as my label control. The program runs ok until I click on a control that calls this sub.
|

12-26-07, 11:27 AM
|
 |
Coding Addict
|
|
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
|
|
As I said before...
1.) What happens when you run the debugger?
2.) Are you attaching the label dynamically?
If you attach the label dynamically, that may not be parsed before you attempt to assign it a value.
Is 'lblCurrentQuestionNumber' a label or a user control?
What happens when you do this:
asp Code:
Dim myLabel As Label = Page.FindControl("lblCurrentQuestionNumber") myLabel.Text = <whatever>
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
|

12-26-07, 11:31 AM
|
 |
Coding Addict
|
|
Join Date: Aug 2006
Location: Baltimore, MD
Posts: 342
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Just realized your calling this from inside the user control, so try this:
asp Code:
Dim myLabel As Label = me.Parent.FindControl("lblCurrentQuestionNumber") myLabel.Text = <whatever>
__________________
"Political Correctness is a doctrine, fostered by a delusionary, illogical, liberal minority and rabidly promoted by an unscrupulous mainstream media, which holds forth the proposition that it is entirely possible to pick up a turd by the clean end."
|

12-26-07, 11:56 AM
|
|
Newbie Coder
|
|
Join Date: Oct 2006
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
If by attaching the label dynamically you mean with code, no, I dragged it onto the form.
'lblCurrentQuestionNumber' is a label not a user control
When I do this, the programs loads ok until I click on a user control that calls the sub routine that contains the following:
Dim myLabel As Label = Me.Parent.Page.FindControl("lblCurrentQuestionNumb er")
myLabel.Text = 1.ToString()
The error message: System.NullReferenceException: Object reference not set to an instance of an object.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|