Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 452
  • Last Modified:

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

0
WebAppDeveloper
Asked:
WebAppDeveloper
  • 3
  • 2
1 Solution
 
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
 
WebAppDeveloperAuthor 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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now