Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > [newbie] need help on this NULL object reference

Reply
Thread Tools

[newbie] need help on this NULL object reference

 
 
Jeff
Guest
Posts: n/a
 
      11-11-2006
Hey

asp.net 2.0

Below is the code I have behind a web page. The problem is during runtime in
btnSave_Click, userProfile has a NULL value. I don't understand why it has a
NULL value, because AFAIK I've declared userProfile as a class variable so
the value it has in btnSave_Click should be the value it did get in
Page_Load. userProfile get a value in Page_Load - but is NULL in
btnSave_Click

What am I doing wrong here?

public partial class EditUser : System.Web.UI.Page
{
private ProfileCommon userProfile;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string username = Request.QueryString["user"].ToString();
if (username.Length > 0)
{
userProfile = Profile.GetProfile(username);
this.Settings1.SetText(userProfile);
}

}
}


protected void btnSave_Click(object sender, EventArgs e)
{
this.Settings1.SaveInfo(userProfile);
}
}


 
Reply With Quote
 
 
 
 
Mark Fitzpatrick
Guest
Posts: n/a
 
      11-11-2006
First, you always need to check your Request.QueryString variables to ensure
they aren't null.

You'll need to add an

if(Request.QueryString["user"] != null)

condition check.

The thing to keep in mind, the userprofile is set when you first load the
page. It is not loaded the second time because you're only loading it if it
isn't a postback. Keep in mind, setting a variable in a web form is not like
setting a variable in a windows form, the state is not maintained betwee
postbacks unless you create a mechanism to save the state of the item. You
could try saving the profile into the viewstate and then fetching it back on
subsequent calls. Possibly by creating a property that looks something like:

private ProfileCommon UserProfile
{
get{
if(ViewState["userprofile"] != null)
return (ProfileCommon)ViewState["userprofile"];
else
return null;
}
set
{
ViewState["userprofile"] = value;
}
}

Will this work? I don't know. I haven't tried to do this one before and I'm
not sure if the profile can be serialized into the viewstate or not. The
bottom line though, unless you can find a way to maintain the state of the
profile you'll have to remove the !Page.Ispostback check.


--
Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006


"Jeff" <> wrote in message
news:%...
> Hey
>
> asp.net 2.0
>
> Below is the code I have behind a web page. The problem is during runtime
> in btnSave_Click, userProfile has a NULL value. I don't understand why it
> has a NULL value, because AFAIK I've declared userProfile as a class
> variable so the value it has in btnSave_Click should be the value it did
> get in Page_Load. userProfile get a value in Page_Load - but is NULL in
> btnSave_Click
>
> What am I doing wrong here?
>
> public partial class EditUser : System.Web.UI.Page
> {
> private ProfileCommon userProfile;
> protected void Page_Load(object sender, EventArgs e)
> {
> if (!this.IsPostBack)
> {
> string username = Request.QueryString["user"].ToString();
> if (username.Length > 0)
> {
> userProfile = Profile.GetProfile(username);
> this.Settings1.SetText(userProfile);
> }
>
> }
> }
>
>
> protected void btnSave_Click(object sender, EventArgs e)
> {
> this.Settings1.SaveInfo(userProfile);
> }
> }
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASPX page jscript rt error: null is null or not an object Cirene ASP .Net 1 06-09-2008 07:59 PM
"Object reference not set to an instance of an object" Weird thing happens with reference a link nguyentrongkha@gmail.com ASP .Net 1 09-20-2007 09:46 PM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57