Class members and NullReferenceException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Irwin

    #1

    Class members and NullReferenceException

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

    public void ReturnResults(s tring codeID)
    {
    Response.Redire ct(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.Retur nResults("6");

    Now, when this gets called a NullReferenceEx ception 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?


  • Jon Skeet [C# MVP]

    #2
    Re: Class members and NullReferenceEx ception

    Mike Irwin <mike@faroutfre akyshit.com> wrote:[color=blue]
    > I've built a class ControlBase that has a member ReturnResults that looks
    > like this:
    >
    > public void ReturnResults(s tring codeID)
    > {
    > Response.Redire ct(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.Retur nResults("6");
    >
    > Now, when this gets called a NullReferenceEx ception 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?[/color]

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Mike Irwin

      #3
      Re: Class members and NullReferenceEx ception


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.19f46c 9854b2d4989865@ msnews.microsof t.com...[color=blue]
      > That sounds very odd - is the NullReferenceEx ception definitely being
      > thrown by ReturnResults and not by the constructor? What does the stack
      > trace look like?[/color]

      The Source Error points to the Reponse.Redirec t call.

      Here's the stack trace:

      [NullReferenceEx ception: Object reference not set to an instance of an
      object.]
      System.Web.UI.U serControl.get_ Response() +7
      Delicious.Contr olBase.ReturnRe sults(String codeID) in C:\Documents and
      Settings\Mike\M y Documents\Visua l Studio
      Projects\Delici ous\ControlBase .cs:54
      Delicious.PageB ase.Page_Load(O bject sender, EventArgs e) in c:\documents
      and settings\mike\m y documents\visua l studio
      projects\delici ous\default.asp x.cs:71
      System.Web.UI.C ontrol.OnLoad(E ventArgs e) +67
      System.Web.UI.C ontrol.LoadRecu rsive() +35
      System.Web.UI.P age.ProcessRequ estMain() +725


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Class members and NullReferenceEx ception

        Mike Irwin <mike@faroutfre akyshit.com> wrote:[color=blue]
        > "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
        > news:MPG.19f46c 9854b2d4989865@ msnews.microsof t.com...[color=green]
        > > That sounds very odd - is the NullReferenceEx ception definitely being
        > > thrown by ReturnResults and not by the constructor? What does the stack
        > > trace look like?[/color]
        >
        > The Source Error points to the Reponse.Redirec t call.[/color]

        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 - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Mike Irwin

          #5
          Re: Class members and NullReferenceEx ception


          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          news:MPG.19f4a5 a89514c3fa98986 a@msnews.micros oft.com...[color=blue]
          > 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.[/color]

          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.


          Comment

          Working...