Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

java.lang.NullPointerException in a CFC

Status
Not open for further replies.

jwdcfdeveloper

Programmer
Mar 20, 2001
170
0
0
US
I am using the following code to create a token to pass to another part of my application:

<cffunction access="public" name="createToken" output="false" returntype="string" hint="Creates a token.">

<cfscript>
//create token generator instance
myInstance = CreateObject("Java", "TokenGenerator");
getInstance = myInstance.getInstance();

//create vars to hold the parameters
URL = "Payload = "my payload";
IP = cgi.REMOTE_HOST;

//create token data instance and add parameters
tokenData = CreateObject("Java", "TokenData");
myURL = tokenData.setURL(JavaCast("String", akamaiURL));
myPayload = tokenData.setPayload(JavaCast("String", akamaiPayload));
myIP = tokenData.setClientIP(JavaCast("String",akamaiIP));

//now tokenize the data
at = myInstance.generateToken("DMC",tokenData);
</cfscript>

<cfreturn akamaiToken>
</cffunction>

I get a null pointer exception when I try and create the token (at). DMC is an xml file that gets other parameters from besides the three that are set here. It works fine up to that point. I ran the actual Java in a main method and it worked fine, so I know it's not the Java. But for whatever reason, it won't run in my CFC. Any suggestions?

Thanks,

JW
 
A number of things. First of URL is a scope and thus a reserved word my guess is you have changed this but your code uses akamaiURL and akamaiPayload which I do not see being set anywhere?

I would remove the MyURL, MyIp variables etc. just for clarity as I'm guessing your set methods return void.

Finally before calling the generateToken method - can you confirm that the values are actually set in your object perhaps by using getURL...etc if you have accessor methods?

Have you checked all the obvious things - method case etc.?
HTH
 
The akamaiURL, akamaiPayload are suppose to be url, payload and, ip respectively. also those are not the actual names in the code (the names were changed to protect the innocent). The getURL, getXXX, methods do return values the code works up until the call to get the generateToken("DMC",tokenData) method. I understand what you are saying, but my issue is beyond a simple typo.

Thanks for replying,

JW
 
Just another suggestion - although you shouldn't need to - does it help if you cast the string before passing it in?
Code:
at = myInstance.generateToken(JavaCast("String",'DMC'),tokenData);
 
I tried that too. I did some research and I think it may be because the method that creates the token is buried quite a few layers down. I read on the macromedia website that sometimes CF code has a problem finding a Java method if it's buried too deeply, CF may not be able to find the code. The suggestion I have heard is to create a CFX tag.
 
Thats interesting I haven't come across that before if that is the case its a bit worrying - if you have a link to anything please let me know as I tend to use java from cf quite a bit

Thanks
 
After talking to a couple of Java developers, I found out that the problem was the XML file a hidden class was trying to read. The xeres.jar & xalan.jar files I have on my computer were different than the ones the original Developers (Java) have. There are few ways I found to fix this:

1. Copy the XML file into a variable as a String (kind of hacky but it works), and run the rest of the code as normal.

2. Use the fileName.properties file to hold the values and change the Java (or have the Java Developer) change the file to be read from the *.properties file.

3. Make sure all .jar files are in sync.

Hope this helps somebody down the road.
 
Cool glad you got to the bottom of this - but I'm curious as to how they found out - you say it worked when called from Java directly right? Did they debug the class in via Coldfusion
 
Nah, they used another IDE probably Elipse. That seems to be the popular Java IDE around here. I myself like NetBeans, perhaps because I'm much more of Java novice .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top