Object reference not set to an instance of an object

Hi,
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">
        <tr>
            <td>
                <asp:ImageButton ID="btnProductA" runat="server" ImageUrl="images/ProductA.gif" BorderWidth="0" />
            </td>
        </tr>
        <tr>
            <td>
                  Click on product picture.
            </td>
        </tr>
    </table>
</asp:Content>
 
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)
    {
        Utilities.RegisterBaseUrl(this.Page);
        ImageButton btnProductA = (ImageButton)FindControl("btnProductA");
        btnProductA.Attributes.Add("onmouseover", "imageOn();");
        btnProductA.Attributes.Add("onmouseout", "imageOff();");
    }
}

Open in new window

WebAppDeveloperAsked:
Who is Participating?
 
WebAppDeveloperConnect With a Mentor Author Commented:
The solutions is: I didn't need this line "ImageButton btnProductA = (ImageButton)FindControl("btnProductA");"    All I needed was: "btnProductA.Attributes.Add("onmouseover", "imageOn();");"
0
 
anarki_jimbelCommented:
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.
0
 
WebAppDeveloperAuthor Commented:
anarki_jimbel,

So how can I fix it?
0
 
anarki_jimbelCommented:
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
0
 
anarki_jimbelCommented:
"The issue is no longer an issue"  - I like that. So, what was the reason? Found the button eventually?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.