473,915 Members | 5,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with "Object reference not set to an instance of an object"

Hi,

I tried to create a class which must change the propety 'visible' of a
<linktag 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").Visib le = True Then'.

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

the class:
---------
Imports Microsoft.Visua lBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim mpg As MasterPage
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
....
End If
End Sub
End Class

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

masterpage.mast er:
------------------
<link runat="server" id="lkred" href="App_Theme s/red.css" rel=Stylesheet
type="text/css" visible="true"/>

Mar 4 '07 #1
35 3269
Chris wrote:
Hi,

I tried to create a class which must change the propety 'visible' of a
<linktag 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").Visib le = True Then'.

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

the class:
---------
Imports Microsoft.Visua lBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page
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.
Dim mpg As MasterPage
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
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.
....
End If
End Sub
End Class

code-behind:
-----------
Partial Class MasterPage
Inherits System.Web.UI.M asterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
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.
End Sub
End Class

masterpage.mast er:
------------------
<link runat="server" id="lkred" href="App_Theme s/red.css" rel=Stylesheet
type="text/css" visible="true"/>

--
Göran Andersson
_____
http://www.guffa.com
Mar 4 '07 #2
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
_____
http://www.guffa.com
Mar 4 '07 #3
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.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = 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

Mar 4 '07 #4
You are only declaring mpg as a variable, not an instance of a MasterPage
class, so when you write:

If mpg.FindControl ("lkred").Visib le = 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

"Chris" <??**@nospam.dc wrote in message
news:el******** ******@TK2MSFTN GP06.phx.gbl...
Hi,

I tried to create a class which must change the propety 'visible' of a
<linktag 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").Visib le = True Then'.

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

the class:
---------
Imports Microsoft.Visua lBasic
Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim mpg As MasterPage
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
....
End If
End Sub
End Class

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

masterpage.mast er:
------------------
<link runat="server" id="lkred" href="App_Theme s/red.css" rel=Stylesheet
type="text/css" visible="true"/>

Mar 4 '07 #5
Chris wrote:
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.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = 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
Here's an example of how you send a reference to a control into a method:

SomeMethod(theC ontrol)

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

Public Sub SomeMethod(link As Control)

--
Göran Andersson
_____
http://www.guffa.com
Mar 5 '07 #6
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").Visib le = 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_Theme s/red/red.css"
rel=Stylesheet type="text/css" visible="true"/>
<link runat="server" id="lkgreen" href="App_Theme s/green/green.css"
rel=Stylesheet type="text/css" visible="false" />
</head>

classe:
-------
Imports Microsoft.Visua lBasic

Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
mpg.FindControl ("lkred").Visib le = False
mpg.FindControl ("lkgreen").Vis ible = True
Else
mpg.FindControl ("lkred").Visib le = True
mpg.FindControl ("lkgreen").Vis ible = False
End If
End If
End Sub
End Class

code-behind of masterpage:
---------------------------
Partial Class MasterPage
Inherits System.Web.UI.M asterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class
Mar 5 '07 #7
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")

"Chris" <??**@nospam.dc wrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
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").Visib le = 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_Theme s/red/red.css"
rel=Stylesheet type="text/css" visible="true"/>
<link runat="server" id="lkgreen" href="App_Theme s/green/green.css"
rel=Stylesheet type="text/css" visible="false" />
</head>

classe:
-------
Imports Microsoft.Visua lBasic

Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
mpg.FindControl ("lkred").Visib le = False
mpg.FindControl ("lkgreen").Vis ible = True
Else
mpg.FindControl ("lkred").Visib le = True
mpg.FindControl ("lkgreen").Vis ible = False
End If
End If
End Sub
End Class

code-behind of masterpage:
---------------------------
Partial Class MasterPage
Inherits System.Web.UI.M asterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class
Mar 5 '07 #8
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@localhos tschreef in bericht
news:%2******** ********@TK2MSF TNGP02.phx.gbl. ..
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")

"Chris" <??**@nospam.dc wrote in message
news:%2******** ********@TK2MSF TNGP06.phx.gbl. ..
>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").Visib le = 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_Theme s/red/red.css"
rel=Styleshe et type="text/css" visible="true"/>
<link runat="server" id="lkgreen" href="App_Theme s/green/green.css"
rel=Styleshe et type="text/css" visible="false" />
</head>

classe:
-------
Imports Microsoft.Visua lBasic

Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
mpg.FindControl ("lkred").Visib le = False
mpg.FindControl ("lkgreen").Vis ible = True
Else
mpg.FindControl ("lkred").Visib le = True
mpg.FindControl ("lkgreen").Vis ible = False
End If
End If
End Sub
End Class

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

Mar 5 '07 #9
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.

Chris wrote:
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").Visib le = 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_Theme s/red/red.css"
rel=Stylesheet type="text/css" visible="true"/>
<link runat="server" id="lkgreen" href="App_Theme s/green/green.css"
rel=Stylesheet type="text/css" visible="false" />
</head>

classe:
-------
Imports Microsoft.Visua lBasic

Public Class loginkl
Public Sub logkl()
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo
If pg.User.Identit y.IsAuthenticat ed = True Then
If mpg.FindControl ("lkred").Visib le = True Then
mpg.FindControl ("lkred").Visib le = False
mpg.FindControl ("lkgreen").Vis ible = True
Else
mpg.FindControl ("lkred").Visib le = True
mpg.FindControl ("lkgreen").Vis ible = False
End If
End If
End Sub
End Class

code-behind of masterpage:
---------------------------
Partial Class MasterPage
Inherits System.Web.UI.M asterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Init
Dim lg As New loginkl
lg.logkl()
End Sub
End Class
--
Göran Andersson
_____
http://www.guffa.com
Mar 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2763
by: Bob Cannistraci | last post by:
A three-tier user authentication system was running without a problem for almost a year and now is suddenly dysfunctional. We don't know of any changes to any of the servers. It's quite maddening. The details: 1) We know the COM+ app is instanced on the COM+ server (Win 2000). The component graphic spins when CreateObject is called. 2) The proxy is installed on an IIS server (Win 2000) with delivers the interface, written in ASP, to the...
5
6849
by: Tommy Lang | last post by:
Why doesn't the following code work? I get an error at the following line... CardGroup.PropertyCardType = (CardType)0; The error is "Object reference not set to an instance of an object.", which means the object in question have not been initiated. But I have init the object above n this line... public Card CardGroup = new Card; Anybody knows how to resolve this??
3
19734
by: Steve Lutz | last post by:
Hi All, I have a Windows Service that runs well. The service hosts a remote object. The purpose of the object is so that I can "peak" into the service to see what it's doing. I wrote a small Windows Application that connects to the remote object and prints out status information. Everything works fine when I first start the service. However, after some period, when I attempt to connect to the remote object, I get "Requested Service not...
1
2837
by: Chris Magoun | last post by:
I suddenly received an unexpected error in my project. I have been working on this project for some time without this issue. Nothing has changed in the form that caused the exception. A little experimentation shows that I cannot run ANY .NET web project without getting this error: ---------------------------------------------------------------------------- ---- Object reference not set to an instance of an object.
18
28770
by: Microsoft | last post by:
When I try this in my code I alwas get an errormessage: "Object reference not set to an instance of an object" Dim g As System.Drawing.Graphics g.DrawString("Test", New Font("Arial", 12, FontStyle.Bold), Brushes.Black, 0, 0) Why is this? Marc
1
2300
by: Gummy | last post by:
Hello, I've been banging my head against the wall for a few days on this. When I run a page, either in "View in Browser" or I actually build the solution, I occasionally and very randomly get the following message: Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and...
2
2983
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back to the same page at the top of my form alerting the user that their request has been approved or denied. However, the end result is an error message: Error Message: Object reference not set to an instance of an object.
35
2092
by: Chris | last post by:
Hi, I tried to create a class which must change the propety 'visible' of a <linktag 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
4
2187
by: My Pet Programmer | last post by:
Ok guys, I'm really looking for someone to tell me how bad a hack this is, and if I'm close to where I should be with it. The basic situation is that I have a class which creates a basic calendar control, the only difference is I stole the navigation scheme from Vista (e.g., if you click on the year you zoom out to the months list, then out to the decade, and back in when you click a year, then a month). I ran into some trouble...
0
10039
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11359
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10543
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9734
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7259
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5944
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4779
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3370
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.