Groups | Blog | Home
all groups > asp.net > march 2007 >

asp.net : problem with "Object reference not set to an instance of an object"


Scott M.
3/4/2007 6:29:43 PM
You are only declaring mpg as a variable, not an instance of a MasterPage
class, so when you write:

If mpg.FindControl("lkred").Visible = True Then

you get the error because "mpg" has not been set to an instance of a
MasterPage class.

You need to either instantiate mpg: Dim mpg As NEW MasterPage

or set mpg equal to an already created instance of a MasterPage:

Dim foo As New MasterPage
Dim mpg As MasterPage = foo

-Scott



[quoted text, click to view]

Chris
3/4/2007 10:41:49 PM
Hi,

I tried to create a class which must change the propety 'visible' of a
<link> tag in the masterpage into 'false' when the user is logged. But i get
the error: "Object reference not set to an instance of an object"
for the line 'If mpg.FindControl("lkred").Visible = True Then'.

I couldn't find sofar the solution.
Any help would be appreciated ...
Thanks
Chris

the class:
---------
Imports Microsoft.VisualBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim mpg As MasterPage
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then
....
End If
End Sub
End Class

code-behind:
-----------
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class

masterpage.master:
------------------
<link runat="server" id="lkred" href="App_Themes/red.css" rel=Stylesheet
type="text/css" visible="true"/>


Göran_Andersson
3/4/2007 11:09:42 PM
[quoted text, click to view]

Here you are creating a completely new instance of the Page class. The
Page class is the base class for pages and doesn't contain any controls
at all.

[quoted text, click to view]

The FindControl method returns a null reference. As there are no
controls in the page object, the control you are looking for can of
course not be found.

[quoted text, click to view]

Here you should to pass a reference to the current Page into the method,
so that it can use that to locate the control.

Better yet, why not pass a reference to the control into the method.
That way you don't have to use FindControl to locate it, and the method
can be used to hide any control that you like, not only a control named
"lkred".

I am not sure, but the Init event of the master page might also occur
too early to access the controls of the page. If the markup of the page
has not yet been parsed, the page does not yet contain any controls.
Then you have to use an even that occurs later in the cycle.

[quoted text, click to view]

--
Göran Andersson
_____
Göran_Andersson
3/4/2007 11:32:51 PM
Correction: I see now that you look for the control in the MasterPage
object, not in the Page object.

The same appplies, though, but the actual error occurs because you have
just declared a reference to a master page, and haven't assign any value
to it. When you try to use the reference, it's null.

(Are you sure that this is the actual code that you are using? I would
expect a compiler error as you are trying to use a variable that hasn't
been assigned any value.)

You have to send a reference into the method, either a reference to the
actual master page, or a reference to the control. The base class for
master pages can not be used to access controls in any specific master page.

--
Göran Andersson
_____
Chris
3/5/2007 12:00:00 AM
Hi, thanks to you too. i did what you told me:but i still have the same
error for the same line "'If mpg.FindControl("lkred").Visible = True Then'.
I show you the whole code to be sure ...(i also tried with Protected Sub
Page_Load instead of Protected Sub Page_Init).


masterpage:
-----------
<head runat="server">
<link runat="server" id="Lkred" href="App_Themes/red/red.css"
rel=Stylesheet type="text/css" visible="true"/>
<link runat="server" id="lkgreen" href="App_Themes/green/green.css"
rel=Stylesheet type="text/css" visible="false" />
</head>

classe:
-------
Imports Microsoft.VisualBasic

Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then
mpg.FindControl("lkred").Visible = False
mpg.FindControl("lkgreen").Visible = True
Else
mpg.FindControl("lkred").Visible = True
mpg.FindControl("lkgreen").Visible = False
End If
End If
End Sub
End Class

code-behind of masterpage:
---------------------------
Partial Class MasterPage
Inherits System.Web.UI.MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class

Stephany Young
3/5/2007 12:00:00 AM
In the masterpage, the id of the control is Lkred and not lkred.

mpg.FindControl("lkred") returns nothing becuase it should be
mpg.FindControl("Lkred")



[quoted text, click to view]
Chris
3/5/2007 12:00:00 AM
Hi,

It's true but it doesn't matter, i thing ... it's not case sensitive
(VB.net) (i tried with both lower and uppercase).
Still same error.


"Stephany Young" <noone@localhost> schreef in bericht
news:%23UWPf8vXHHA.4000@TK2MSFTNGP02.phx.gbl...
[quoted text, click to view]

Göran_Andersson
3/5/2007 12:00:00 AM
As I have explained already, you can not access the controls of a
specific master page by creating an instance of the base class for
master pages. You need a reference to the actual master page that
contains the control.

Read what I have written in my other posts in this thread about sending
a reference to the method.

[quoted text, click to view]

--
Göran Andersson
_____
Chris
3/5/2007 12:20:47 AM
Hi, thanks

I changed this line in the class:
Dim mpg As New MasterPage

I have no error anymore, but instead of changing the theme from red into
green, it remains red. So at least one test remains false:
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then

Could you please give me the right code for doing what you told me?
The same appplies, though, but the actual error occurs because you have
just declared a reference to a master page, and haven't assign any value
to it.

and

You have to send a reference into the method, either a reference to the
actual master page, or a reference to the control

thanks again


Göran_Andersson
3/5/2007 1:33:56 AM
[quoted text, click to view]

Here's an example of how you send a reference to a control into a method:

SomeMethod(theControl)

Here's how you declare the method to accept the reference:

Public Sub SomeMethod(link As Control)

--
Göran Andersson
_____
AddThis Social Bookmark Button