Object reference not set to an instance of an object

Posted on 2009-04-30
Last Modified: 2012-05-06
I have the following code that keeps giving me an error "System.NullReferenceException: Object reference not set to an instance of an object." on the line "btnProductA.Attributes.Add("onmouseover", "imageOn();");".  The code (giving the error) is in a content page of a master page.  How can I resolve this?

Thanks very much in advance!
Here's the code in the Content Page (which is embedded within a Master Page):


<asp:Content ID="Content1" ContentPlaceHolderID="ProductImages" Runat="Server">

    <table border="0" cellpadding="0" cellspacing="0">



                <asp:ImageButton ID="btnProductA" runat="server" ImageUrl="images/ProductA.gif" BorderWidth="0" />





                  Click on product picture.






Here's the code behind of the Content Page:


using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;


public partial class customers : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)



        ImageButton btnProductA = (ImageButton)FindControl("btnProductA");

        btnProductA.Attributes.Add("onmouseover", "imageOn();");

        btnProductA.Attributes.Add("onmouseout", "imageOff();");



Open in new window

Question by:WebAppDeveloper
    LVL 29

    Expert Comment

    Obviously the line ImageButton btnProductA = (ImageButton)FindControl("btnProductA");
    does not return an object. I.e the button is nothing. Probably there is some problem with the FindControl method.

    Author Comment


    So how can I fix it?
    LVL 29

    Expert Comment

    Good question. I don't know what the FindControl function is doing exactly and how it work. Honestly speaking, I have no idea how control search works for web pages - I work mainly with winforms.

    If FindControl is a custom method - try to debug it and see why it returns null.

    But probably it would be a good idea to set a break point at
    LVL 29

    Expert Comment

    "The issue is no longer an issue"  - I like that. So, what was the reason? Found the button eventually?

    Accepted Solution

    The solutions is: I didn't need this line "ImageButton btnProductA = (ImageButton)FindControl("btnProductA");"    All I needed was: "btnProductA.Attributes.Add("onmouseover", "imageOn();");"

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Article by: Ivo
    Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    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…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 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

    15 Experts available now in Live!

    Get 1:1 Help Now