Object reference not set to an instance of an object.

Posted on 2009-12-16
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
    LVL 41

    Accepted Solution

    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
    LVL 4

    Expert Comment

    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

    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!
    LVL 10

    Assisted Solution

    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.
    LVL 4

    Assisted Solution

    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 not the same as...

    Protected NextLink as New LinkButton

    Author Comment

    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

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

    Author Closing Comment


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    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…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now