Link to home
Start Free TrialLog in
Avatar of Bobby X
Bobby XFlag for United States of America

asked on

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

Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

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.
Avatar of Bobby X

ASKER

anarki_jimbel,

So how can I fix it?
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
"The issue is no longer an issue"  - I like that. So, what was the reason? Found the button eventually?
ASKER CERTIFIED SOLUTION
Avatar of Bobby X
Bobby X
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial