Raymond Camden's Blog Rss

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

18

Posted in ColdFusion | Posted on 09-12-2007 | 3,586 views

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:

view plain print about
1<cffunction
2 name = "onRequest"
3 access = "public"
4 returnType = "boolean"
5 output="true">

6
7 <cfargument
8 name = "thePage"
9 type = "string"
10 required = "true">

11
12 <cfinclude
13 template="#arguments.thePage#">

14
15 <cfreturn true />
16
17 </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

[Add Comment] [Subscribe to 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 ?
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?
What do you mean by blank OnRequest? The code above is anything but blank? o_O
Blank as in it doesn't do anything.
Wouldn't that be:
<cffunction name="onRequest">
</cffunction>

Your onRequest code example isn't blank, thus... is doing something. Hence my confusion. :)
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).
onRequest goes in Application.cfc.
Todd,

It goes in application.cfc =)
I thought so. I guess I'm confused because that's what my onRequest normally looks like except the returntype isn't boolean.
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.
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.
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.
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.
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!
So now that's two of us!

Terrific! But ... who is going to explain to us why in the world this worked?
@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.
I tried this and for some reason it shut off the style sheets on my site.
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!

[Add Comment] [Subscribe to Comments]