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

C

Chris

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:
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
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

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.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = 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.MasterPage
Protected Sub Page_Init(ByVal sender As Object, ByVal e As
System.EventArgs) 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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
 
C

Chris

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
 
S

Scott M.

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
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
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

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)
 
C

Chris

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
 
S

Stephany Young

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")
 
C

Chris

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.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

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.
 
C

Chris

I tried it without class, defining everything in the code-behind of the
master page and it works.
If Page.User.Identity.IsAuthenticated Then
If Lkred.Visible = True Then
Lkred.Visible = False
lkgreen.Visible = True
Else
Lkred.Visible = True
lkgreen.Visible = False
End If
End If


I want to learn how to do the same with a class, but sorry, i can't find the
right code working with a class.
I tried a lot of things without succes:

- in web.config, i added a reference: <pages
masterPageFile="~/MasterPage.master">
- this is the updated class file:
Public Class loginkl
Public Sub logkl(ByVal link As Control)
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

- the updated code-behind of master page
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

Can anybody tell me finally what i need to change in this precise code?
Thanks
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Chris said:
I tried it without class, defining everything in the code-behind of the
master page and it works.
If Page.User.Identity.IsAuthenticated Then
If Lkred.Visible = True Then
Lkred.Visible = False
lkgreen.Visible = True
Else
Lkred.Visible = True
lkgreen.Visible = False
End If
End If


I want to learn how to do the same with a class, but sorry, i can't find the
right code working with a class.
I tried a lot of things without succes:

- in web.config, i added a reference: <pages
masterPageFile="~/MasterPage.master">
- this is the updated class file:
Public Class loginkl
Public Sub logkl(ByVal link As Control)

As you want to change two controls, you want to send to controls to the
method:

Public Sub logkl(link1 As Control, link2 as Control)
Dim pg As New Page
Dim foo As New MasterPage
Dim mpg As MasterPage = foo

Skip the masterpage references. You don't need them as you have
reference to the controls.
If pg.User.Identity.IsAuthenticated = True Then
If mpg.FindControl("lkred").Visible = True Then

Use the reference to the control instead of searching for it:

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

Same here:

link1.Visible = False
link2.Visible = True
Else
mpg.FindControl("lkred").Visible = True
mpg.FindControl("lkgreen").Visible = False

link1.Visible = True
link2.Visible = False
End If
End If
End Sub
End Class

- the updated code-behind of master page
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Init
Dim lg As New loginkl
lg.logkl(???)

lg.logkl(Lkred, lkgreen)
 
C

Chris

Hi Goran,

Thanks, it works now..

Göran Andersson said:
As you want to change two controls, you want to send to controls to the
method:

Public Sub logkl(link1 As Control, link2 as Control)


Skip the masterpage references. You don't need them as you have reference
to the controls.


Use the reference to the control instead of searching for it:

If link1.Visible Then


Same here:

link1.Visible = False
link2.Visible = True


link1.Visible = True
link2.Visible = False


lg.logkl(Lkred, lkgreen)
 
S

Scott M.

When dealing with string literals (any value in quotes), case always
matters. It's not a VB thing.

For example:

"Scott" does not equal "scott", so:

Dim A As String = "Scott"
Bim B As String = "scott"

If A = B Then
MessageBox(A & "= " & B)
Else
MessageBox(A & " <> " & B)
End If

You will find that the code in the Else always runs.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

The reason that the code in the Else always runs is not because of the
case of the string, but because of the fact that the Controls collection
is always empty.
 
S

Scott M.

I am not talking about controls in master pages Goran. My message is in
direct response to a remark about strings not being case sensitive in VB
that was made by Chris.

My example below does not have anything to do with control collections.
 
C

Cor Ligthert [MVP]

Scott,

Your message should be true, however, it is not always, it is strange enough
that in AspNet the string literals from ADONET can be mixed uper and lower
case. (I thought I had seen this in version 1.1).

Cor
 
S

Scott M.

I don't think so Cor,

A string literal is always just that.

You may be referring to situations when you refer to a field name in a
database using a case that does not match the capitalization of the actual
field name.

For example, assume the following SQL statement:

SELECT * From tblCustomers WHERE NAME = "Smith"

It's up to the engine interpreting the strings to decide if it wishes to
enforce case-sensitivity. Many databases do not enforce it on field names,
but do enforce it when querying on a particular value (the WHERE clause of a
SQL statement). In other words, if the field name or string in the WHERE
clause isn't treated case-sensitively, it's because the database engine
processing the SQL statement did it that way, not because the language the
SQL statement was part of (VB.NET, C#, etc.) isn't case-sensitive.

-Scott
 
C

Cor Ligthert [MVP]

Scott,

It is something I have tried in past and I don't do it again.
The same code gave errors in windowsforms and was eaten by AspNet.
I found it very strange.

If you don't believe me, does not matter for me, it you keeps you on the
rule than you will never see it.

Cor
 
S

Scott M.

Cor, you haven't given any specific example of "something I have tried in
the past", so I can't really speak to it.

The fact is that VB.NET strings ARE case-sensitive, always. This is not to
say that every class treats strings case-sensitively. It's that old
discussion about the difference between the Framework and the language.

As you know, strings are reference types (objects) and so, they are
instanced and stored on the heap. Each string is a different object (with
the exception of interned strings - - different topic) and we are not
talking about evaluating if one string object "IS" the same as another. We
are talking about the value of one string being equivellant to another. To
the CLR, the char "S" is not equivellant to the char "s" - that doesn't
change, ever.

Now, could you run into a class that takes a string as an input value in one
of its methods and could that method deal with that string in a non
case-sensitive way? Sure, but that is a very different thing from saying
that VB.NET strings are not case-sensitive, which is what Chris said:

" it's not case sensitive (VB.net)"


-Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top