Intermittent Null Pointer error
Copy link to clipboard
Copied
I'm in the process of building a website for my wife's new business. Sometimes when I browse to the website, I get the following error:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.
[snipped links to resources and user agent stuff]
java.lang.NullPointerException
It only happens occasionally, and usually when I'm checking the page in a new browser.
Any ideas as to what could be causing it?
The only unusual part of my setup is that I've called the page header and footer from Application.cfc, like so:
<cffunction
name="OnRequest"
access="public"
returntype="void"
output="true"
hint="Fires after pre page processing is complete.">
<cfargument
name="TargetPage"
type="string"
required="true" />
<cfinclude template="includes/header.cfm" />
<cfinclude template="#ARGUMENTS.TargetPage#" />
<cfinclude template="includes/footer.cfm" />
<cfreturn />
</cffunction>
Perhaps this could be causing problems?
Copy link to clipboard
Copied
BreakawayPaul wrote:
java.lang.NullPointerException
That is just ColdFusion's (and it's underlining Java) telling you that you have used a variable that the progam is no idea about. I.E. An Undefined Variable.
If this is intermittant, somewhere in your code is a variable that does is not always getting defined when the application is executing.
Copy link to clipboard
Copied
Yeah, I think it might have been #ARGUMENTS.TargetPage# that wasn't getting defined. I added a default="index.cfm" to the <cfargument tag above, so we'll see if that helps.
Copy link to clipboard
Copied
BreakawayPaul wrote:
Yeah, I think it might have been #ARGUMENTS.TargetPage# that wasn't getting defined.
That variable comes from the event handler when ColdFusion fires the onRequest event.
I would have looked into the header.cfm and footer.cfm files first and see what variables are in those files and how they are being used.
Finally, there is usually a fairly clear line number when this type of error is thrown. Do you have "Enable Robust Exception Information" turned on in your development environment ColdFusion Administrator?
Copy link to clipboard
Copied
Unfortunately, I don't have access to the administrator since this is shared hosting. I wonder if I could capture the line number using a <cferror> tag and write the error messages to a log.
The header is chock full 'o variables. There's one for the page title, which is pulled from the database. There are a few for my automatic breadcrumb script. Plus, the header contains two more includes. One for the navigation (that uses variables) and one for the aforementioned breadcrumb script.
That line number sure would be helpful! That said, the error has been conspicuously absent since I added the default="index.cfm" as a parameter to that <cfargument> tag.
Copy link to clipboard
Copied
BreakawayPaul wrote:
I wonder if I could capture the line number using a <cferror> tag and write the error messages to a log.
Yes, but even better more specific would be
<cftry>
... problematic code
<cfcatch type="any">
<cfdump var="#cfcatch#">
<!--- and|or log the exception data --->
</cfcatch>
</cftry>
The #cfcatch# sturcture generated by a try-catch block is basically the exact same (and I beleive the source of) the exception data that would be shown if robust exception information was turned on in the administrator.
You could also make use of the onError event handler of the modern Application.cfc framework to simalar and global affect.
JUST DO NOT LEAVE EITHER OF THESE ON IN A PRODUCTION ENVORNMENT! One should never output exception data to the screen in a production envornment.
Copy link to clipboard
Copied
I modified your idea a bit:
<cftry>
...Suspect code...
<cfcatch type="any">
<cfsavecontent variable="savedump">
<cfdump var="#cfcatch#" expand="yes">
</cfsavecontent>
<cffile action="write" file="cferror.log" output="#savedump#" >
</cfcatch>
</cftry>
Let's see what we catch
Copy link to clipboard
Copied
Not a bad idea, just a couple of pointers.
<cftry><cfcatch> will prevent the exception from stoping the processing. If you added this to the body of the onRequest method in your original code, this would lead to a blank page being returned the the user. Depending on the user, this may or may not be an issue, but a simple message output in the <cfcatch...> block may be a bit friendlier.
Plus you may want to abort processing in the catch if there is potential of issues if this chunk of code does not do it's job. Or provide some default result for the piece of code wrapped by the try block that future processing can expect.
Copy link to clipboard
Copied
Got the error. For some reason, it's in two places where I call CGI.SCRIPT_NAME
For some reason, this variable isn't being populated. Do you know of an alternative? I'm trying to get the path of the current page, sans the domain name (ex: /contact/index.cfm)
Copy link to clipboard
Copied
BreakawayPaul wrote:
Got the error. For some reason, it's in two places where I call CGI.SCRIPT_NAME
For some reason, this variable isn't being populated. Do you know of an alternative? I'm trying to get the path of the current page, sans the domain name (ex: /contact/index.cfm)
You should be able to use some combination of the getCurrentTemplatePath(), getDirectoryFromPath() and|or getFileFromPath(). These will be system FILE path, but it should be simple to truncate to the URL path portion.
Much of the CGI scope comes from the browser or the web server and can very from type to type.
Copy link to clipboard
Copied
The problem with that is that it kept giving me the name of the incude (i.e. /includes/header.cfm) instead of the name of the current page. But after some fiddling, I came up with this:
<cffunction name="fetchpath" output="No" access="public">
<cfset thisurl = getPageContext().getRequest().getRequestURI()>
<cfreturn thisurl>
</cffunction>
Which I can invoke with #fetchpath()#
Thanks ilssac! I think I'm all fixed up now.
Copy link to clipboard
Copied
BreakawayPaul wrote:
The problem with that is that it kept giving me the name of the incude
A quick look at similar functions should have also shown you the getBaseTemplatePath() which would do similar to your pagecontext functions.

