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