nullreferenceexception for Private Object Variables

G

Guest

Hi There,

I have created a very simple web user control, containing only a combo box.
I have created a class which contains some private object variables, for
example...

Private _anObject as Object

Public Property anObject() As Object
Get
Return _anObject
End Get
Set(ByVal value As Object)
_anObject = value
End Set
End Property

I set the AnObject object from the main ASP form, and the local private
_anObject object is set correctly.

If I want to refer to this private _anObject object later, when the user
clicks on a combo box item, I get a NULLREFERENCEEXCEPTION, and the private
object is back to nothing again.

Protected Sub ComboClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Combo1.SelectedIndexChanged
dim myNewObject as Object=_anObject
End Sub

Why is the happening, and what is the correct way to store objects in
classes in ASP.NET?
 
M

Marina

HTTP is a stateless protocol. A new instance of the page object is created
on every request. So on a postback, you get a brand new instance of the page
and everything else on it. That object on which you set the property before
no longer exists.
 
S

Steven Cheng[MSFT]

Hi David,

I agree with Marina on this. Since the ASP.NET web page is based on HTTp
which is stateless, so the page object model (instance member values....)
will not be persisted between multiple requests ( postback...). Generally
for mantaining states between multiple requests, we need to store those
variables in some persistent store such as SessionState or ViewState......

for example:

using ViewState.....

Public Property anObject() As Object
Get
Return ViewState("myobject")
End Get
Set(ByVal value As Object)
ViewState("myobject") = value
End Set
End Property


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





--------------------
| From: "Marina" <[email protected]>
| References: <[email protected]>
| Subject: Re: nullreferenceexception for Private Object Variables
| Date: Tue, 13 Dec 2005 11:38:48 -0500
| Lines: 42
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <u#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: gw.deltek.com 63.72.155.97
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364526
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| HTTP is a stateless protocol. A new instance of the page object is
created
| on every request. So on a postback, you get a brand new instance of the
page
| and everything else on it. That object on which you set the property
before
| no longer exists.
|
| | > Hi There,
| >
| > I have created a very simple web user control, containing only a combo
| > box.
| > I have created a class which contains some private object variables, for
| > example...
| >
| > Private _anObject as Object
| >
| > Public Property anObject() As Object
| > Get
| > Return _anObject
| > End Get
| > Set(ByVal value As Object)
| > _anObject = value
| > End Set
| > End Property
| >
| > I set the AnObject object from the main ASP form, and the local private
| > _anObject object is set correctly.
| >
| > If I want to refer to this private _anObject object later, when the user
| > clicks on a combo box item, I get a NULLREFERENCEEXCEPTION, and the
| > private
| > object is back to nothing again.
| >
| > Protected Sub ComboClick(ByVal sender As Object, ByVal e As
| > System.EventArgs) Handles Combo1.SelectedIndexChanged
| > dim myNewObject as Object=_anObject
| > End Sub
| >
| > Why is the happening, and what is the correct way to store objects in
| > classes in ASP.NET?
|
|
|
 
G

Guest

Thank you both for replying to the question.

The trouble is, the object is not serializable, and is actually an object
which needs to be logged into, and a number of methods performed before
access to the properties is granted. Does Viewstates and SessionStates
actually store objects themselves, or just XML representations of them?

Thanks again,

David
 
M

Marina

In process session state can store the object as is in memory, no
serialization. View State serializes the object.
 
G

Guest

Oh that's great! Thanks a lot for you help, and will read all about session
states now!

David.
 
S

Steven Cheng[MSFT]

Thanks for Marina's input.

Hi David,

If you have any further question, please feel free to post here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Marina" <[email protected]>
| References: <[email protected]>
<u#[email protected]>
<[email protected]>
<[email protected]>
| Subject: Re: nullreferenceexception for Private Object Variables
| Date: Wed, 14 Dec 2005 09:16:08 -0500
| Lines: 127
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: gw.deltek.com 63.72.155.97
| Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA03.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP
08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:364752
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| In process session state can store the object as is in memory, no
| serialization. View State serializes the object.
|
| | > Thank you both for replying to the question.
| >
| > The trouble is, the object is not serializable, and is actually an
object
| > which needs to be logged into, and a number of methods performed before
| > access to the properties is granted. Does Viewstates and SessionStates
| > actually store objects themselves, or just XML representations of them?
| >
| > Thanks again,
| >
| > David
| >
| > "Steven Cheng[MSFT]" wrote:
| >
| >> Hi David,
| >>
| >> I agree with Marina on this. Since the ASP.NET web page is based on
HTTp
| >> which is stateless, so the page object model (instance member
values....)
| >> will not be persisted between multiple requests ( postback...).
Generally
| >> for mantaining states between multiple requests, we need to store those
| >> variables in some persistent store such as SessionState or
| >> ViewState......
| >>
| >> for example:
| >>
| >> using ViewState.....
| >>
| >> Public Property anObject() As Object
| >> Get
| >> Return ViewState("myobject")
| >> End Get
| >> Set(ByVal value As Object)
| >> ViewState("myobject") = value
| >> End Set
| >> End Property
| >>
| >>
| >> Hope helps. Thanks,
| >>
| >> Steven Cheng
| >> Microsoft Online Support
| >>
| >> Get Secure! www.microsoft.com/security
| >> (This posting is provided "AS IS", with no warranties, and confers no
| >> rights.)
| >>
| >>
| >>
| >>
| >>
| >> --------------------
| >> | From: "Marina" <[email protected]>
| >> | References: <[email protected]>
| >> | Subject: Re: nullreferenceexception for Private Object Variables
| >> | Date: Tue, 13 Dec 2005 11:38:48 -0500
| >> | Lines: 42
| >> | X-Priority: 3
| >> | X-MSMail-Priority: Normal
| >> | X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| >> | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| >> | X-RFC2646: Format=Flowed; Original
| >> | Message-ID: <u#[email protected]>
| >> | Newsgroups: microsoft.public.dotnet.framework.aspnet
| >> | NNTP-Posting-Host: gw.deltek.com 63.72.155.97
| >> | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| >> | Xref: TK2MSFTNGXA02.phx.gbl
| >> microsoft.public.dotnet.framework.aspnet:364526
| >> | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| >> |
| >> | HTTP is a stateless protocol. A new instance of the page object is
| >> created
| >> | on every request. So on a postback, you get a brand new instance of
the
| >> page
| >> | and everything else on it. That object on which you set the property
| >> before
| >> | no longer exists.
| >> |
| >> | | >> | > Hi There,
| >> | >
| >> | > I have created a very simple web user control, containing only a
| >> combo
| >> | > box.
| >> | > I have created a class which contains some private object
variables,
| >> for
| >> | > example...
| >> | >
| >> | > Private _anObject as Object
| >> | >
| >> | > Public Property anObject() As Object
| >> | > Get
| >> | > Return _anObject
| >> | > End Get
| >> | > Set(ByVal value As Object)
| >> | > _anObject = value
| >> | > End Set
| >> | > End Property
| >> | >
| >> | > I set the AnObject object from the main ASP form, and the local
| >> private
| >> | > _anObject object is set correctly.
| >> | >
| >> | > If I want to refer to this private _anObject object later, when
the
| >> user
| >> | > clicks on a combo box item, I get a NULLREFERENCEEXCEPTION, and the
| >> | > private
| >> | > object is back to nothing again.
| >> | >
| >> | > Protected Sub ComboClick(ByVal sender As Object, ByVal e As
| >> | > System.EventArgs) Handles Combo1.SelectedIndexChanged
| >> | > dim myNewObject as Object=_anObject
| >> | > End Sub
| >> | >
| >> | > Why is the happening, and what is the correct way to store objects
in
| >> | > classes in ASP.NET?
| >> |
| >> |
| >> |
| >>
| >>
|
|
|
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top