By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

C# - Custom class as return value for function

P: n/a
I have a class file in my ASP .NET C# application - defined as follows:

namespace ABC {
public class BizObj {
public Style GetVal(int id) {
Style temp = new Style();
// run some db queries
temp.theName = reader["firstName"].toString();
return temp;
}
}

public class Style {
public string theName;
}
}

In the code behind of my aspx page, I do the following:

Style style = new Style();
BizObj bizobj = new BizObj();
style = bizObj.GetVal(1);
somelabel.text = style.theName;

When I compile and access my page, I get the following error :

Object reference not set to an instance of an object

The line number it refers to is where I set the label's text field.
When i hard code a value (somelabel.text = "Hello";) it works fine. Any
ideas??

Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi,

The only errors that I see are toString should be ToString, bizObj should be
bizobj and somelabel.text should be somelabel.Text (remember that C# is case
sensitive). Once I fixed these it ran fine on my machine. Does your app
have access to the ABC namespace?

If you continue to have the error can you post more of your code along with
a more detailed error trace?

Hope this helps.

Nov 19 '05 #2

P: n/a
> Object reference not set to an instance of an object

This can only mean one thing: In your code, you're referencing an object
that is null.

As you've posted the line of code that threw the exception (thank you!),
let's see how many object references are in it:

somelabel.text = style.theName

I'm going to assume that "somelabel" is not null, because of your
hard-coding experiment. However, C# is case-sensitive, so "somelabel.text"
should throw an exception. It should read "somelabel.Text" instead.

Still, that might have been a typo on your part. I noticed that in your
"success" code, it is not capitalized either, and you don't say that this
threw an exception.

So, at this point, we have only 2 candidates left: "style" and
"style.theName." While it is possible that your call to "bizObj.GetVal()"
may be returning a null value, I kind of doubt it, unless some database
operation failed, and you're suppressing Exceptions. However, it is possible
that "theName" is null, if, for example, it is null in the database.

On the other hand, I don't know what your programming environment is, and
"Style" is the name of the System.Web.UI.WebControls Style class, so you
seem to have an ambiguous reference there with regards to the name "Style."
Still, as you pointed out, it didn't throw any exceptions when you
hard-coded "Hello" into your statement.

As a result of your experiment, I would have to guess that "theName" is the
culprit in your code, and is null, for whatever reason.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.

<b_*****@yahoo.ca> wrote in message
news:11*********************@c13g2000cwb.googlegro ups.com...
I have a class file in my ASP .NET C# application - defined as follows:

namespace ABC {
public class BizObj {
public Style GetVal(int id) {
Style temp = new Style();
// run some db queries
temp.theName = reader["firstName"].toString();
return temp;
}
}

public class Style {
public string theName;
}
}

In the code behind of my aspx page, I do the following:

Style style = new Style();
BizObj bizobj = new BizObj();
style = bizObj.GetVal(1);
somelabel.text = style.theName;

When I compile and access my page, I get the following error :

Object reference not set to an instance of an object

The line number it refers to is where I set the label's text field.
When i hard code a value (somelabel.text = "Hello";) it works fine. Any
ideas??

Nov 19 '05 #3

P: n/a
Thanks for pointing out that Style is defined elsewhere.. I renamed the
class to MyStyle.. but still got the error..

So i added some error checking code and it turns out style variable is
returned as null.. So thats the prob.

Thx.

Bijoy

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.