Link to home
Start Free TrialLog in
Avatar of mmedi005
mmedi005

asked on

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. is the ERROR i get, why?

My class

Imports Microsoft.VisualBasic
Imports System.Web.UI

Public Class menu

    Public Shared Sub Create_Side_Bars(ByVal x As Integer)

        Dim Page As New Page
        Dim myImage As Image = CType(Page.Master.FindControl("img_left_bar"), Image)
        myImage.Height = x

    End Sub

End Class

ERROR:


Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

And highlights this line

Line 8:          Dim myImage As Image = CType(Page.Master.FindControl("img_left_bar"), Image)

Can anyone help me?
Avatar of samtran0331
samtran0331
Flag of United States of America image

try removing this line:
Dim Page As New Page
when you do this:
Dim myImage As Image = CType(Page.Master.FindControl("img_left_bar"), Image)

the "Page" is already defined as the current page you're working with
It can't find the control called "img_left_bar". Either you are doing this too early in the page life cycle an need to wait until Page_PreRender, or the control "img_left_bar" is in a subcontrol of the Page.Master.
For example if in the masterpage you have:

<asp:Panel id="something" runat="server">

    <asp:image id="img_left_bar" ruant="server" imageurl="something.jpg"/>

</asp:Panel>


You might need to do this:

((Panel)Page.Master.FindControl("something")).FindControl("img_left_bar")
Avatar of mmedi005
mmedi005

ASKER

I in the transition of going from asp to asp .net, would a Panel be the right way to go?  I have thimage inside a div.  Reason why is because the control I have with the CSS. Can I manipulate the position of a div with a panel.  

I do keep getting errors where ever I place the Sub routine. Pre Render- Pre Load, etc...

Keeps referring that Page is Null.....  
>>Keeps referring that Page is Null....
.  
When you do this:
 
       Dim Page As New Page
        Dim myImage As Image = CType(Page.Master.FindControl("img_left_bar"), Image)

You:
1. Instantiate a page.
2. try to find the master page of the page you just instantiated.

the page exists, but only the instance in the first line...it doesn't actually exist as a real "something.aspx" page...that's why you're getting the error.
But if I don't set it to new, visual studio complains about a possible runtime error if I just have

Dim myImage As Image = CType(Page.Master.FindControl("img_left_bar"), Image)
                                                    ------

and it does error out....

so basically, i can not do this in a class, it would have to be in the actual webpage?
if you do it in a class, then you can pass the page to the class function...like...

class file:

Public Sub SetImage(byval ThePage as system.web.ui.page)
Dim myImage As Image = CType(ThePage.Master.FindControl("img_left_bar"), Image)
end sub
ASKER CERTIFIED SOLUTION
Avatar of samtran0331
samtran0331
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
also this:
http://aspnet.4guysfromrolla.com/articles/041305-1.aspx

between the 2 articles, you can piece together a way to use a base page class; which also works for master pages and you can create a base master page for code..and how to pass properties between a master/content pages