System.NullReferenceException - But just look where...

O

Oddball

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

pfc = new PrivateFontCollection();

Here's the stack:

System.Drawing.SafeNativeMethods.GdipNewPrivateFontCollection(IntPtr&
fontCollection) +0
System.Drawing.Text.PrivateFontCollection..ctor()
Adquiesco2.RenderFont.Page_Load(Object sender, EventArgs e) in d:\web
design\active sites\adquiesco2\renderfont.aspx.cs:34
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


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#N0SP4M#wd.co.uk
 
J

Jon Skeet [C# MVP]

Oddball said:
Why on this earth is the following line of code causing a NullReferenceException?

pfc = new PrivateFontCollection();

Here's the stack:

System.Drawing.SafeNativeMethods.GdipNewPrivateFontCollection(IntPtr&
fontCollection) +0
System.Drawing.Text.PrivateFontCollection..ctor()
Adquiesco2.RenderFont.Page_Load(Object sender, EventArgs e) in d:\web
design\active sites\adquiesco2\renderfont.aspx.cs:34
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


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!

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

J.Marsch

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

Willy Denoyette [MVP]

Oddball said:
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
NullReferenceException?

pfc = new PrivateFontCollection();

Here's the stack:

System.Drawing.SafeNativeMethods.GdipNewPrivateFontCollection(IntPtr&
fontCollection) +0
System.Drawing.Text.PrivateFontCollection..ctor()
Adquiesco2.RenderFont.Page_Load(Object sender, EventArgs e) in d:\web
design\active sites\adquiesco2\renderfont.aspx.cs:34
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


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#N0SP4M#wd.co.uk

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

Willy.
 
O

Oddball

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 said:
Jon is right, these classes can't/shouldn't be used from web server
applications. What do you trying to achieve?

Willy.


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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
O

Oddball

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 said:
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 NullReferenceException?

pfc = new PrivateFontCollection();

Here's the stack:

System.Drawing.SafeNativeMethods.GdipNewPrivateFontCollection(IntPtr&
fontCollection) +0
System.Drawing.Text.PrivateFontCollection..ctor()
Adquiesco2.RenderFont.Page_Load(Object sender, EventArgs e) in d:\web
design\active sites\adquiesco2\renderfont.aspx.cs:34
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()


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#N0SP4M#wd.co.uk


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

Another unchecked rambeling brought to you by:

Oddball
joshua@bf#N0SP4M#wd.co.uk
 
W

Willy Denoyette [MVP]

Oddball said:
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.

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

J.Marsch

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

Willy Denoyette [MVP]

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

J.Marsch

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top