Current location: Hot Scripts Forums » Programming Languages » ASP.NET » reference not set to an instance of an object. Please help!


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

Reply
  #1 (permalink)  
Old 12-25-07, 10:31 AM
DonLindon DonLindon is offline
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?
Reply With Quote
  #2 (permalink)  
Old 12-26-07, 09:58 AM
omniman's Avatar
omniman omniman is offline
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:
  1. Dim tb As TextBox = <parent control>.FindControl("lblCurrentQuestionNumber")
  2.  
  3. 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."
Reply With Quote
  #3 (permalink)  
Old 12-26-07, 10:28 AM
DonLindon DonLindon is offline
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
Reply With Quote
  #4 (permalink)  
Old 12-26-07, 10:34 AM
omniman's Avatar
omniman omniman is offline
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.

Code:
lblCurrentQuestionNumber.Text = inComingNum.ToString
Where is 'lblCurrentQuestionNumber'? Is it wrapped in anything like a repeater, or detailsView, etc...?

If not, you can use:
Code:
Page.FindControl("lblCurrentQuestionNumber")
__________________
"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."
Reply With Quote
  #5 (permalink)  
Old 12-26-07, 10:49 AM
DonLindon DonLindon is offline
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?
Reply With Quote
  #6 (permalink)  
Old 12-26-07, 10:55 AM
omniman's Avatar
omniman omniman is offline
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."
Reply With Quote
  #7 (permalink)  
Old 12-26-07, 11:12 AM
DonLindon DonLindon is offline
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.
Reply With Quote
  #8 (permalink)  
Old 12-26-07, 11:27 AM
omniman's Avatar
omniman omniman is offline
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:
  1. Dim myLabel As Label = Page.FindControl("lblCurrentQuestionNumber")
  2. 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."
Reply With Quote
  #9 (permalink)  
Old 12-26-07, 11:31 AM
omniman's Avatar
omniman omniman is offline
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:
  1. Dim myLabel As Label = me.Parent.FindControl("lblCurrentQuestionNumber")
  2. 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."
Reply With Quote
  #10 (permalink)  
Old 12-26-07, 11:56 AM
DonLindon DonLindon is offline
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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Object reference not set to an instance of an object. Cabal Windows .NET Programming 2 02-15-07 05:40 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM
Error in OleDBConnection : Object reference not set to an instance of an object pvsunil Windows .NET Programming 1 04-22-05 01:45 PM
java set random weight in instance joebloggs2004 Everything Java 1 09-24-04 09:37 AM
Object Oriented Programming Stefan PHP 29 12-30-03 11:22 AM


All times are GMT -5. The time now is 02:35 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.