• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 326
  • Last Modified:

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?
0
mmedi005
Asked:
mmedi005
  • 6
  • 2
  • 2
1 Solution
 
samtran0331Commented:
try removing this line:
Dim Page As New Page
0
 
samtran0331Commented:
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
0
 
strickddCommented:
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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
strickddCommented:
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")
0
 
mmedi005Author Commented:
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.....  
0
 
samtran0331Commented:
>>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.
0
 
mmedi005Author Commented:
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?
0
 
samtran0331Commented:
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
0
 
samtran0331Commented:
oops and to add to my last comment, on the content page you could then use:

Dim c as myClassThatHasTheSetImageSub
c.SetImage(Me.Page)

if you don't want to instantiate the class, make it a Public Shared Function and on the contentpage, you can:
myClassThatHasTheSetImageSub.SetImage(Me.Page)
without having to Dim an instance of the class

You might find this useful:
http://aspnet.4guysfromrolla.com/articles/013107-1.aspx
0
 
samtran0331Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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