System.NullReferenceException - But just look where...

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

    System.NullReferenceException - But just look where...


    Firstly I'd like to say hello. This is my first post having ghosted for some time - I know,
    I'm a bad man.

    I'm pretty sure this following post hasn't been adressed on the forum, I may be wrong -
    it's quite large.

    Why on this earth is the following line of code causing a NullReferenceEx ception?

    pfc = new PrivateFontColl ection();

    Here's the stack:

    System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
    fontCollection) +0
    System.Drawing. Text.PrivateFon tCollection..ct or()
    Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
    design\active sites\adquiesco 2\renderfont.as px.cs:34
    System.Web.UI.C ontrol.OnLoad(E ventArgs e)
    System.Web.UI.C ontrol.LoadRecu rsive()
    System.Web.UI.P age.ProcessRequ estMain()


    This has got me *confused*. It looks like some code *inside* the System.Drawing
    collection is messing up... but surely this is caused by something I have done. The line
    I have shown you is literaly the first line of code executed in the on Page_Load
    method. Nothing happens before it!


    ------------------------------------

    Another unchecked rambeling brought to you by:

    Oddball
    joshua@bf#N0SP4 M#wd.co.uk
  • Jon Skeet [C# MVP]

    #2
    Re: System.NullRefe renceException - But just look where...

    Oddball <joshua@SbPfAwM d.co.uk> wrote:[color=blue]
    > Why on this earth is the following line of code causing a NullReferenceEx ception?
    >
    > pfc = new PrivateFontColl ection();
    >
    > Here's the stack:
    >
    > System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
    > fontCollection) +0
    > System.Drawing. Text.PrivateFon tCollection..ct or()
    > Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
    > design\active sites\adquiesco 2\renderfont.as px.cs:34
    > System.Web.UI.C ontrol.OnLoad(E ventArgs e)
    > System.Web.UI.C ontrol.LoadRecu rsive()
    > System.Web.UI.P age.ProcessRequ estMain()
    >
    >
    > This has got me *confused*. It looks like some code *inside* the System.Drawing
    > collection is messing up... but surely this is caused by something I have done. The line
    > I have shown you is literaly the first line of code executed in the on Page_Load
    > method. Nothing happens before it![/color]

    My guess is that as it's a GDI+ class, ASP.NET may be having trouble
    using it as a service - services don't tend to do anything with
    graphic-type resources.

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

    • J.Marsch

      #3
      Re: System.NullRefe renceException - But just look where...

      That's rather bizarre. I wonder if it could be a security problem?

      You could try this:
      before starting your application, run this command: caspol.exe -security off

      Then start your application (the command only affects applications after you
      start them, so don't cheat <g>).

      If you start working after that, then you at least know that you need to
      look at .net security for the answer.

      BTW: don't forget to run caspol.exe -security on when you are finished.


      "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
      news:jFkpe.4687 $jS3.2935@newsf e2-win.ntli.net...[color=blue]
      >
      > Firstly I'd like to say hello. This is my first post having ghosted for
      > some time - I know,
      > I'm a bad man.
      >
      > I'm pretty sure this following post hasn't been adressed on the forum, I
      > may be wrong -
      > it's quite large.
      >
      > Why on this earth is the following line of code causing a
      > NullReferenceEx ception?
      >
      > pfc = new PrivateFontColl ection();
      >
      > Here's the stack:
      >
      > System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
      > fontCollection) +0
      > System.Drawing. Text.PrivateFon tCollection..ct or()
      > Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
      > design\active sites\adquiesco 2\renderfont.as px.cs:34
      > System.Web.UI.C ontrol.OnLoad(E ventArgs e)
      > System.Web.UI.C ontrol.LoadRecu rsive()
      > System.Web.UI.P age.ProcessRequ estMain()
      >
      >
      > This has got me *confused*. It looks like some code *inside* the
      > System.Drawing
      > collection is messing up... but surely this is caused by something I have
      > done. The line
      > I have shown you is literaly the first line of code executed in the on
      > Page_Load
      > method. Nothing happens before it!
      >
      >
      > ------------------------------------
      >
      > Another unchecked rambeling brought to you by:
      >
      > Oddball
      > joshua@bf#N0SP4 M#wd.co.uk[/color]


      Comment

      • Willy Denoyette [MVP]

        #4
        Re: System.NullRefe renceException - But just look where...


        "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
        news:jFkpe.4687 $jS3.2935@newsf e2-win.ntli.net...[color=blue]
        >
        > Firstly I'd like to say hello. This is my first post having ghosted for
        > some time - I know,
        > I'm a bad man.
        >
        > I'm pretty sure this following post hasn't been adressed on the forum, I
        > may be wrong -
        > it's quite large.
        >
        > Why on this earth is the following line of code causing a
        > NullReferenceEx ception?
        >
        > pfc = new PrivateFontColl ection();
        >
        > Here's the stack:
        >
        > System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
        > fontCollection) +0
        > System.Drawing. Text.PrivateFon tCollection..ct or()
        > Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
        > design\active sites\adquiesco 2\renderfont.as px.cs:34
        > System.Web.UI.C ontrol.OnLoad(E ventArgs e)
        > System.Web.UI.C ontrol.LoadRecu rsive()
        > System.Web.UI.P age.ProcessRequ estMain()
        >
        >
        > This has got me *confused*. It looks like some code *inside* the
        > System.Drawing
        > collection is messing up... but surely this is caused by something I have
        > done. The line
        > I have shown you is literaly the first line of code executed in the on
        > Page_Load
        > method. Nothing happens before it!
        >
        >
        > ------------------------------------
        >
        > Another unchecked rambeling brought to you by:
        >
        > Oddball
        > joshua@bf#N0SP4 M#wd.co.uk[/color]

        Jon is right, these classes can't/shouldn't be used from web server
        applications. What do you trying to achieve?

        Willy.


        Comment

        • Oddball

          #5
          Re: System.NullRefe renceException - But just look where...


          The classes seem to work fine as I had them working a good 24 hours ago. I've finaly
          worked out what's wrong. I'll get to that in a second because I have a cause not a
          solution.

          The aim of this code is to turn a custom font which we created into an image for
          streaming to a page. The GDI+ classes are used on webpages to create things like
          Thumbnails, to resize and reorientate images on the fly. If you have a database of
          images you don't want to be having to store massive, large, medium, small and
          thumbnail versions of every image.

          "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote:
          [color=blue]
          >
          >"Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
          >news:jFkpe.468 7$jS3.2935@news fe2-win.ntli.net...[color=green]
          >>
          >> Firstly I'd like to say hello. This is my first post having ghosted for
          >> some time - I know,
          >> I'm a bad man.
          >>
          >> I'm pretty sure this following post hasn't been adressed on the forum, I
          >> may be wrong -
          >> it's quite large.
          >>
          >> Why on this earth is the following line of code causing a
          >> NullReferenceEx ception?
          >>
          >> pfc = new PrivateFontColl ection();
          >>
          >> Here's the stack:
          >>
          >> System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
          >> fontCollection) +0
          >> System.Drawing. Text.PrivateFon tCollection..ct or()
          >> Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
          >> design\active sites\adquiesco 2\renderfont.as px.cs:34
          >> System.Web.UI.C ontrol.OnLoad(E ventArgs e)
          >> System.Web.UI.C ontrol.LoadRecu rsive()
          >> System.Web.UI.P age.ProcessRequ estMain()
          >>
          >>
          >> This has got me *confused*. It looks like some code *inside* the
          >> System.Drawing
          >> collection is messing up... but surely this is caused by something I have
          >> done. The line
          >> I have shown you is literaly the first line of code executed in the on
          >> Page_Load
          >> method. Nothing happens before it!
          >>
          >>
          >> ------------------------------------
          >>
          >> Another unchecked rambeling brought to you by:
          >>
          >> Oddball
          >> joshua@bf#N0SP4 M#wd.co.uk[/color]
          >
          >Jon is right, these classes can't/shouldn't be used from web server
          >applications . What do you trying to achieve?
          >
          >Willy.
          >[/color]


          ------------------------------------

          Another unchecked rambeling brought to you by:

          Oddball
          joshua@bf#N0SP4 M#wd.co.uk

          Comment

          • Oddball

            #6
            Re: System.NullRefe renceException - But just look where...


            Right - the problem is being caused by some unsafe code that is furter down the page.
            It turns out to be VERY unsafe as the program runs MOST of the way through the first
            time, SOME of the way the second time and STALLS on the first line after that. What
            appears to be happening is that the code I have written is destroying the .NET
            framework on my PC.

            Think I'll delete my unsafe code for work at a later date and replace it with a slower,
            managed version.


            "Oddball" <joshua@SbPfAwM d.co.uk> wrote:
            [color=blue]
            >
            >Firstly I'd like to say hello. This is my first post having ghosted for some time - I know,
            >I'm a bad man.
            >
            >I'm pretty sure this following post hasn't been adressed on the forum, I may be wrong -
            >it's quite large.
            >
            >Why on this earth is the following line of code causing a NullReferenceEx ception?
            >
            >pfc = new PrivateFontColl ection();
            >
            >Here's the stack:
            >
            > System.Drawing. SafeNativeMetho ds.GdipNewPriva teFontCollectio n(IntPtr&
            >fontCollection ) +0
            > System.Drawing. Text.PrivateFon tCollection..ct or()
            > Adquiesco2.Rend erFont.Page_Loa d(Object sender, EventArgs e) in d:\web
            >design\activ e sites\adquiesco 2\renderfont.as px.cs:34
            > System.Web.UI.C ontrol.OnLoad(E ventArgs e)
            > System.Web.UI.C ontrol.LoadRecu rsive()
            > System.Web.UI.P age.ProcessRequ estMain()
            >
            >
            >This has got me *confused*. It looks like some code *inside* the System.Drawing
            >collection is messing up... but surely this is caused by something I have done. The[/color]
            line[color=blue]
            >I have shown you is literaly the first line of code executed in the on Page_Load
            >method. Nothing happens before it!
            >
            >
            >------------------------------------
            >
            >Another unchecked rambeling brought to you by:
            >
            >Oddball
            >joshua@bf#N0SP 4M#wd.co.uk[/color]


            ------------------------------------

            Another unchecked rambeling brought to you by:

            Oddball
            joshua@bf#N0SP4 M#wd.co.uk

            Comment

            • Willy Denoyette [MVP]

              #7
              Re: System.NullRefe renceException - But just look where...


              "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
              news:gDmpe.1288 $q46.184@newsfe 1-win.ntli.net...[color=blue]
              >
              > The classes seem to work fine as I had them working a good 24 hours ago.
              > I've finaly
              > worked out what's wrong. I'll get to that in a second because I have a
              > cause not a
              > solution.
              >
              > The aim of this code is to turn a custom font which we created into an
              > image for
              > streaming to a page. The GDI+ classes are used on webpages to create
              > things like
              > Thumbnails, to resize and reorientate images on the fly. If you have a
              > database of
              > images you don't want to be having to store massive, large, medium, small
              > and
              > thumbnail versions of every image.
              >
              > "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote:
              >[/color]

              The fact that they work doesnt mean they should work all the time, it
              depends on the environment, normaly spoken Windows.Forms and Windows.Drawing
              classes aren't meant to be used server side.
              Don't say we didn't warn you if it breaks.

              Willy.



              Comment

              • J.Marsch

                #8
                Re: System.NullRefe renceException - But just look where...

                He should be OK using GDI+ on the web. In fact, there are both books and
                articles out there that detail the use of GDI+ for doing just what the
                poster is discussing -- dynamic creation thumbnails and such.

                Now, I agree that we should probably not be using anything in the
                System.Windows namespace for the web, but note that the drawing namespace is
                System.Drawing, not Windows.Drawing , and based on a couple of graphics books
                that I have lying around here, that's intentional.


                "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                news:uVBzcY6aFH A.2076@TK2MSFTN GP15.phx.gbl...[color=blue]
                >
                > "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
                > news:gDmpe.1288 $q46.184@newsfe 1-win.ntli.net...[color=green]
                >>
                >> The classes seem to work fine as I had them working a good 24 hours ago.
                >> I've finaly
                >> worked out what's wrong. I'll get to that in a second because I have a
                >> cause not a
                >> solution.
                >>
                >> The aim of this code is to turn a custom font which we created into an
                >> image for
                >> streaming to a page. The GDI+ classes are used on webpages to create
                >> things like
                >> Thumbnails, to resize and reorientate images on the fly. If you have a
                >> database of
                >> images you don't want to be having to store massive, large, medium, small
                >> and
                >> thumbnail versions of every image.
                >>
                >> "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote:
                >>[/color]
                >
                > The fact that they work doesnt mean they should work all the time, it
                > depends on the environment, normaly spoken Windows.Forms and
                > Windows.Drawing classes aren't meant to be used server side.
                > Don't say we didn't warn you if it breaks.
                >
                > Willy.
                >
                >
                >[/color]


                Comment

                • Willy Denoyette [MVP]

                  #9
                  Re: System.NullRefe renceException - But just look where...

                  No It's not OK, despite your books and articles. Following snippet if from
                  the latest MSDN docs for Whidbey.
                  <
                  Caution
                  Classes within the System.Drawing namespace are not supported for use within
                  a Windows or ASP.NET service. Attempting to use these classes from within
                  one of these application types may produce unexpected problems, such as
                  diminished service performance and run-time exceptions.[color=blue]
                  >[/color]

                  It's very unfortunate that it took 4 years this to be included as a
                  "Caution". IMO it should be classified as "DANGER", "unexpected problems"
                  and "run-time" exceptions is something you really don't want when running
                  asp.net, don't you.

                  Willy.


                  "J.Marsch" <jmarsch@newsgr oup.nospam> wrote in message
                  news:upLq0UrcFH A.2688@TK2MSFTN GP14.phx.gbl...[color=blue]
                  > He should be OK using GDI+ on the web. In fact, there are both books and
                  > articles out there that detail the use of GDI+ for doing just what the
                  > poster is discussing -- dynamic creation thumbnails and such.
                  >
                  > Now, I agree that we should probably not be using anything in the
                  > System.Windows namespace for the web, but note that the drawing namespace
                  > is
                  > System.Drawing, not Windows.Drawing , and based on a couple of graphics
                  > books
                  > that I have lying around here, that's intentional.
                  >
                  >
                  > "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                  > news:uVBzcY6aFH A.2076@TK2MSFTN GP15.phx.gbl...[color=green]
                  >>
                  >> "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
                  >> news:gDmpe.1288 $q46.184@newsfe 1-win.ntli.net...[color=darkred]
                  >>>
                  >>> The classes seem to work fine as I had them working a good 24 hours ago.
                  >>> I've finaly
                  >>> worked out what's wrong. I'll get to that in a second because I have a
                  >>> cause not a
                  >>> solution.
                  >>>
                  >>> The aim of this code is to turn a custom font which we created into an
                  >>> image for
                  >>> streaming to a page. The GDI+ classes are used on webpages to create
                  >>> things like
                  >>> Thumbnails, to resize and reorientate images on the fly. If you have a
                  >>> database of
                  >>> images you don't want to be having to store massive, large, medium,
                  >>> small
                  >>> and
                  >>> thumbnail versions of every image.
                  >>>
                  >>> "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote:
                  >>>[/color]
                  >>
                  >> The fact that they work doesnt mean they should work all the time, it
                  >> depends on the environment, normaly spoken Windows.Forms and
                  >> Windows.Drawing classes aren't meant to be used server side.
                  >> Don't say we didn't warn you if it breaks.
                  >>
                  >> Willy.
                  >>
                  >>
                  >>[/color]
                  >
                  >[/color]




                  Comment

                  • J.Marsch

                    #10
                    Re: System.NullRefe renceException - But just look where...

                    I stand corrected.

                    It is very unfortunate that it took this long for it to come out -- there
                    are quite a few examples out there of using these technologies together. I
                    haven't verified the articles yet, but there look to be serveral of them
                    online right now in MSDN magazine articles (search for GDI+ and ASP.net).


                    "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                    news:eXfY$9wcFH A.2288@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > No It's not OK, despite your books and articles. Following snippet if from
                    > the latest MSDN docs for Whidbey.
                    > <
                    > Caution
                    > Classes within the System.Drawing namespace are not supported for use
                    > within a Windows or ASP.NET service. Attempting to use these classes from
                    > within one of these application types may produce unexpected problems,
                    > such as diminished service performance and run-time exceptions.[color=green]
                    >>[/color]
                    >
                    > It's very unfortunate that it took 4 years this to be included as a
                    > "Caution". IMO it should be classified as "DANGER", "unexpected problems"
                    > and "run-time" exceptions is something you really don't want when running
                    > asp.net, don't you.
                    >
                    > Willy.
                    >
                    >
                    > "J.Marsch" <jmarsch@newsgr oup.nospam> wrote in message
                    > news:upLq0UrcFH A.2688@TK2MSFTN GP14.phx.gbl...[color=green]
                    >> He should be OK using GDI+ on the web. In fact, there are both books and
                    >> articles out there that detail the use of GDI+ for doing just what the
                    >> poster is discussing -- dynamic creation thumbnails and such.
                    >>
                    >> Now, I agree that we should probably not be using anything in the
                    >> System.Windows namespace for the web, but note that the drawing namespace
                    >> is
                    >> System.Drawing, not Windows.Drawing , and based on a couple of graphics
                    >> books
                    >> that I have lying around here, that's intentional.
                    >>
                    >>
                    >> "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote in message
                    >> news:uVBzcY6aFH A.2076@TK2MSFTN GP15.phx.gbl...[color=darkred]
                    >>>
                    >>> "Oddball" <joshua@SbPfAwM d.co.uk> wrote in message
                    >>> news:gDmpe.1288 $q46.184@newsfe 1-win.ntli.net...
                    >>>>
                    >>>> The classes seem to work fine as I had them working a good 24 hours
                    >>>> ago.
                    >>>> I've finaly
                    >>>> worked out what's wrong. I'll get to that in a second because I have a
                    >>>> cause not a
                    >>>> solution.
                    >>>>
                    >>>> The aim of this code is to turn a custom font which we created into an
                    >>>> image for
                    >>>> streaming to a page. The GDI+ classes are used on webpages to create
                    >>>> things like
                    >>>> Thumbnails, to resize and reorientate images on the fly. If you have a
                    >>>> database of
                    >>>> images you don't want to be having to store massive, large, medium,
                    >>>> small
                    >>>> and
                    >>>> thumbnail versions of every image.
                    >>>>
                    >>>> "Willy Denoyette [MVP]" <willy.denoyett e@telenet.be> wrote:
                    >>>>
                    >>>
                    >>> The fact that they work doesnt mean they should work all the time, it
                    >>> depends on the environment, normaly spoken Windows.Forms and
                    >>> Windows.Drawing classes aren't meant to be used server side.
                    >>> Don't say we didn't warn you if it breaks.
                    >>>
                    >>> Willy.
                    >>>
                    >>>
                    >>>[/color]
                    >>
                    >>[/color]
                    >
                    >
                    >[/color]


                    Comment

                    Working...