NPEs, onRequest, and other mysteries of the universe...

A friend wrote to me this morning with an interesting problem. He recently moved his site (hosted at CrystalTech) from ColdFusion 7 to ColdFusion 8. Then he began to get NPEs (Null Pointer Exceptions) in regards to cfquery tags. He would get these about once per 1-2k hits.

But get this - CystalTech recommended adding a blank onRequest:

<cffunction
name = "onRequest"
access = "public"
returnType = "boolean"
output="true">

<cfargument
name = "thePage"
type = "string"
required = "true">

<cfinclude
template="#arguments.thePage#">

<cfreturn true />

</cffunction>

And it worked! He no longer got the NPE error. Now I don't know about you - but I can't imagine any reason why this change would make his error go away. Can anyone else think of a reason why it would help?

As for the NPE in general - one thing I've asked him if is the cfquery was in a CFC and he possibly forgot to var scope. Not that I think it would make a NPE, but the lack of var scoping is something that could lead to random errors like he observed.

Comments

Theres been a few CF issues in the past that have caused this. One I remember was having an extends in your cfc and forgetting to put anything (heh).

Isn't the only thing special about onRequest() that its a mixin for application.cfc ?
# Posted By DK | 9/12/07 11:22 AM
Ray, these are all queries within the pages called, none in CFCs. And it was behavior spread out across several different scripts throughout the site, some with <cfqueryparam> where clauses but others with no WHERE clause at all.

Almost all were tightly wrapped in CRTRY blocks which spit out the NPE error message, although a few on some older, but still active, pages were not in CFTRY blocks and the NPE error message came through the onError method.

Curious, yes?
# Posted By James Edmunds | 9/12/07 12:31 PM
What do you mean by blank OnRequest? The code above is anything but blank? o_O
# Posted By Todd Rafferty | 9/12/07 12:39 PM
Blank as in it doesn't do anything.
# Posted By Raymond Camden | 9/12/07 12:41 PM
Wouldn't that be:
<cffunction name="onRequest">
</cffunction>

Your onRequest code example isn't blank, thus... is doing something. Hence my confusion. :)
# Posted By Todd Rafferty | 9/12/07 12:43 PM
Also, sorry to add this at the last minute, where is he putting this blank onRequest? To the CFC with the queries on it? It's not clear above (to me).
# Posted By Todd Rafferty | 9/12/07 12:46 PM
onRequest goes in Application.cfc.
# Posted By Raymond Camden | 9/12/07 12:48 PM
Todd,

It goes in application.cfc =)
# Posted By Justice | 9/12/07 12:53 PM
I thought so. I guess I'm confused because that's what my onRequest normally looks like except the returntype isn't boolean.
# Posted By Todd Rafferty | 9/12/07 1:00 PM
Well, the onRequest function is a gray area for me, I haven't seen too many uses for it, other than perhaps this example. lol

It is my understanding that it generally cannot be cocmpletely empty because you would have to include the page you were wanting to process for it to be rendered.
# Posted By Shane Zehnder | 9/12/07 1:20 PM
Oh man, I often do security in my onRequest area. Then I can check if the page requested requires security, or redirect them to a totally different area to login, etc. I also have an argument of the page requested, so after login I can direct them back where they wanted to go. Its a great area =) You could even wrap template information before and after your executed page, and do a header / footer type of thing with it.
# Posted By Justice | 9/12/07 2:07 PM
Ok? I do security in my OnRequestStart before the OnRequest has even been started. I understand that I can do a lot and I do a lot, but I leave my OnRequest as generic as possible at the moment. My header/footer is a custom tag on the actual cfm pages themselves. I guess I could push that up one level, but there are usually things being initalized in the shell (navigation, specific javascript files, etc). I guess it's time to write up a child tag for the custom tag.
# Posted By Todd Rafferty | 9/12/07 2:17 PM
I was on a CF7 server at CT and I had that issue, they moved me to a new CF7 server and the issue went away.

I have now moved one of my sites to CF8 for testing and have not had an issue - I also use CFC'c.
# Posted By Dave Dugdale | 9/12/07 6:13 PM
I have been living with NPE's exactly like these for months, after poring over my CFCs for non-var'ed variables and other errors. I already had an OnRequest function in my Application.cfc, but after reading this post I decided to try adding a cfreturn. You know what? No more NPE's! Woohoo!
# Posted By Tom Mollerus | 9/14/07 11:25 AM
So now that's two of us!

Terrific! But ... who is going to explain to us why in the world this worked?
# Posted By James Edmunds | 9/14/07 11:28 AM
@Justice - just wanted to mention another good use for security in onRequest is just to <cfinclude../> your login page rather then including Arguments.requestedPage.

This way the visitor doesn't have to be redirected to a login page, the URL in their browser always stays the same.
# Posted By Greg Stevens | 9/14/07 1:34 PM
I tried this and for some reason it shut off the style sheets on my site.
# Posted By Ian | 9/23/07 10:32 PM
I have the same problem with CF 8 on HostMySite. I implemented the suggested fix on this page, but it did not fix the problem. I found something in a CF 8 HotFix that sounded similar to my problem, I installed the HotFix, but STILL no luck.

If anyone knows of any other possible solutions, I'm all ears.

Thanks!
# Posted By hjoreteg | 9/27/07 3:16 PM