473,378 Members | 1,541 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,378 software developers and data experts.

Class members and NullReferenceException

I've built a class ControlBase that has a member ReturnResults that looks
like this:

public void ReturnResults(string codeID)
{
Response.Redirect(RESULTS_URL_PREFIX + codeID);
}

I'm accessing the member like this in a user control that does not inherit
from ControlBase:

ControlBase myControl = new ControlBase();
myControl.ReturnResults("6");

Now, when this gets called a NullReferenceException is thrown. If a user
control does inherit from ControlBase I'm able to call this member with no
problems.

Can someone please explain why this is happening?
Nov 15 '05 #1
4 1637
Mike Irwin <mi**@faroutfreakyshit.com> wrote:
I've built a class ControlBase that has a member ReturnResults that looks
like this:

public void ReturnResults(string codeID)
{
Response.Redirect(RESULTS_URL_PREFIX + codeID);
}

I'm accessing the member like this in a user control that does not inherit
from ControlBase:

ControlBase myControl = new ControlBase();
myControl.ReturnResults("6");

Now, when this gets called a NullReferenceException is thrown. If a user
control does inherit from ControlBase I'm able to call this member with no
problems.

Can someone please explain why this is happening?


That sounds very odd - is the NullReferenceException definitely being
thrown by ReturnResults and not by the constructor? What does the stack
trace look like?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP**********************@msnews.microsoft.com ...
That sounds very odd - is the NullReferenceException definitely being
thrown by ReturnResults and not by the constructor? What does the stack
trace look like?


The Source Error points to the Reponse.Redirect call.

Here's the stack trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
System.Web.UI.UserControl.get_Response() +7
Delicious.ControlBase.ReturnResults(String codeID) in C:\Documents and
Settings\Mike\My Documents\Visual Studio
Projects\Delicious\ControlBase.cs:54
Delicious.PageBase.Page_Load(Object sender, EventArgs e) in c:\documents
and settings\mike\my documents\visual studio
projects\delicious\default.aspx.cs:71
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +725
Nov 15 '05 #3
Mike Irwin <mi**@faroutfreakyshit.com> wrote:
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP**********************@msnews.microsoft.com ...
That sounds very odd - is the NullReferenceException definitely being
thrown by ReturnResults and not by the constructor? What does the stack
trace look like?


The Source Error points to the Reponse.Redirect call.


Ah - it looks like it can't get the response. Sorry, I'd forgotten that
Response was a property. I'm not sure that it's going to work when you
just create your own control instance like that, with no request or
response to actually work with. Unfortunately I'm far from an expert on
ASP.NET, so I could be way off base, but that's my first reaction.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Ah - it looks like it can't get the response. Sorry, I'd forgotten that
Response was a property. I'm not sure that it's going to work when you
just create your own control instance like that, with no request or
response to actually work with. Unfortunately I'm far from an expert on
ASP.NET, so I could be way off base, but that's my first reaction.


That makes sense. I guess I need to do some refactoring. Anyway, thanks
for your help.

If anyone has any ideas on how to get this to work, I'm all ears.
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
10
by: Brian | last post by:
I'm reasonably new to C#, and using a class containing a hashtable collection to feed a treeview. My problem is that I can't pull the data from the hashtable! Here's the code I have: if...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
0
by: Shaman | last post by:
hi, im working on this app that makes a treenode view out of data from an xml page, yet theres a few pieces of code that just will not get fixed by me. Anybody help me (code below 'Class...
0
by: razilon | last post by:
Hi, I've written a managed class that makes use of stl vectors of a few unmanaged structs for data handling/manipulation, but I'm getting a few very strange errors. I get an "Unhandled...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
3
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.