473,394 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

C# - Custom class as return value for function

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
3 5325
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Nick | last post by:
I am writing an application that uses a custom class which inherits DictionaryBase so that I can add items with an associated key. When I try to serialize the object though, I get the following...
9
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the...
7
by: Joe Rigley | last post by:
Hi, I have a custom class with a public method. I want to perform a repose.redirect if an error occurs in the public method GetUserRoles. Unfortunately, Visual Studio 2003 is throwing an error...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
2
by: zak | last post by:
I have a custom class object which I'd like bound to text boxes etc on the form. All properties of the class look something like this <System.ComponentModel.Description("Get/Set value associated...
1
by: Josue Avila M. | last post by:
HI I Design a Class Wich contains severals DataTypes . (2 Strings and Other Data Types from a Class wich i Desing to) Its Something like This <serializable()> Public Class Emitor Public...
2
by: info | last post by:
Hi... I want to know (inside a function) if the calling code wants a return value or not. Here is an example: function foo() {
6
by: Bryan | last post by:
I am writing a class that has a list of strings as one of its properties. I want to be able to run some code whenever that list of strings is modified through the class property. Here is the...
3
by: wendallsan | last post by:
Hi All, I've stumped myself writing an app that uses Prototype and a bit of PHP. Here is what I have: I have a custom class named Default_county_init_data that, upon initialization makes...
1
by: Mitch | last post by:
I have several classes that inherit other classes. I need to be able to sort the Mesh class based on its inherited list of Poly.ZMax,. How do I use IComparer or IComparable to achieve this? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.