Object reference not set to an instance of an object.

Posted on 2009-12-16
Medium Priority
Last Modified: 2012-05-08
In the code behind shown (.aspx page included as well), NextLink.Visible = false (Line 16) is giving the following error:

"System.NullReferenceException: Object reference not set to an instance of an object."

I assume that is occuring b/c the LinkButton is inside of the <HeaderTemplate> on a repeater... Is there any way to keep the LinkButtons inside the HeaderTemplate?  If so, please provide code required.

Public Class PagingCodeBehind
Inherits Page
Protected FirstLink As LinkButton
Protected PreviousLink As LinkButton
Protected NextLink As LinkButton
Protected LastLink As LinkButton
Sub Paging(Optional WhichPage As Integer = 1,Optional 
' If current page is the last page, hide the "next" and "last" navigation links
If CurrentPage = Pages Then
	NextLink.Visible = false
	LastLink.Visible = false
' Otherwise, show the "next" and "last" navigation links and set the page index each will pass when clicked
	NextLink.Visible = true
	LastLink.Visible = true
	NextLink.CommandArgument = CurrentPage + 1
	LastLink.CommandArgument = Pages
End If
End Sub
Index page:
<form runat="server">
<asp:LinkButton runat="server" ID="NextLink" Text="Next &raquo;" onClick="ChangePage" />
<asp:LinkButton runat="server" ID="LastLink" Text="Last &raquo;" onClick="ChangePage" />

Open in new window

Question by:webdevelop
  • 3
  • 2
  • 2
  • +1
LVL 41

Accepted Solution

graye earned 675 total points
ID: 26063788
In your example, you left off the most important part.... the part where you instantiate the LinkButtons (ie NextLink = New LinkButton)
We'd need to see that part before we can diagnose the rest

Expert Comment

ID: 26063897
I've never used a repeater before, but I'm thinking that you will end up with multiple "NextLink" and "LastLink" LinkButtons on your page. They will probably need to be rendered with different IDs otherwise you'll likely get an Invalid PostBack error. Having said that, I don't think you can reference these link buttons directly on the server side. You're probably going to need to iterate through the repeated items, declare a reference variable to the LinkButtons, and set the visibility as you iterate through the repeated items.

Also, I've run into issues using the Visible property sometimes (it actually causes the object not to be rendered in HTML at all). Most of the time, I just apply the "display = hidden" style instead. That way, the item still gets rendered and it is available for client side access if need be later... it's just hidden.

Author Comment

ID: 26064103
So line 8 isn't doing that?  Sorry, those are the only lines that reference NextLink and it worked perfectly before I tried to move the code inside of the headertemplate for UI reasons.

The headertemplate/footertemplate regions don't repeat, just the itemtemplate region.

I appreciate any further help!

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

LVL 10

Assisted Solution

codingbeaver earned 150 total points
ID: 26064661
NextLink is defined in HeaderTemplate, I don't think you can directly access it. You will need to use FindControl to find it first, then you can use it.

Assisted Solution

TMarkham1 earned 675 total points
ID: 26065683
If the Link buttons don't repeat, you should be able to access them directly by name.

Like graye mentioned, make sure you instantiate the LinkButtons.

Protected NextLink as LinkButton

...is not the same as...

Protected NextLink as New LinkButton

Author Comment

ID: 26065749
The solution for me was to take the buttons out of the headertemplate, and set the default visibility on each tag I needed control over.  Then, in the code behind I updated code to control visibility on each tag.

As far as I can tell, it can't be done inside the headertemplate.
LVL 10

Expert Comment

ID: 26065797
>>>As far as I can tell, it can't be done inside the headertemplate.
As I said in my last comment.

Author Closing Comment

ID: 31666837

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

840 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