Class members and NullReferenceException

  • Thread starter Thread starter Mike Irwin
  • Start date Start date
M

Mike Irwin

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?
 
Mike Irwin said:
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 said:
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
 
Mike Irwin said:
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 said:
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.
 
Back
Top