POIUtility - Object Instantiation Exception

Posted October 19, 2007 at 7:40 AM

Tags: ColdFusion

Every now and then, I get people contacting me about problems using my POIUtility.cfc. They are usually getting this error:

Object Instantiation Exception. An exception occurred when instantiating a java object.

While I think there might be several reasons that this object instantiation could occur, usually, with the POI Utility ColdFusion component, I find that it is due to the file path that's being used. The POI Utility requires a full file path. This means that you should be using something like ExpandPath( FILE ) when defining your file path. Running this code:

 Launch code in new window » Download code as text file »

  • <cfset objSheet = objPOIUtility.ReadExcel(
  • FilePath = "meals.xls"
  • ) />

... will throw that object instantiation error since "meals.xls" is not a full path. However, running this code, with the full path, will not throw any error and will work just fine:

 Launch code in new window » Download code as text file »

  • <cfset objSheet = objPOIUtility.ReadExcel(
  • FilePath = ExpandPath( "meals.xls" )
  • ) />

Hope that helps with people having this problem.

Download Code Snippet ZIP File

Comments (30)  |  Post Comment  |  Ask Ben  |  Permalink  |  Print Page



Adobe ColdFusion 8.0.1 Update - Helping Programmers To Be Signifanctly Less Girlie - Download ColdFusion 8 Update 8.0.1 Now.

Reader Comments

Heh, every time my project manager has an old copy of a site moved or cloned and doesn't remember to open up the settings file and re-adjusts paths, I get a "Hey... can you look into this Object Instantiation Error?" I send an email back saying, "Hey, did you fix the temp/library paths?"

Posted by Todd Rafferty on Oct 19, 2007 at 8:59 AM


Ooooh managers!

Posted by Ben Nadel on Oct 19, 2007 at 9:28 AM


...? When you're making the big bucks and you're at the top of the food chain like, Mr. Nadel, Chief Technology Officer, wouldn't you have managers?

Posted by Todd Rafferty on Oct 19, 2007 at 9:39 AM


Ha ha, I think until you own your own company, everyone answers to someone else ;)

Posted by Ben Nadel on Oct 19, 2007 at 10:22 AM


I'm actually on equal footing with the project manager and I answer to the owner. :| Sucks being lead developer sometimes.

Posted by Todd Rafferty on Oct 19, 2007 at 10:34 AM


Ive also received that error when i have the dang xls file open on my browser. This only occurs when i try to read in a file. Simple solution to that problem.

Posted by Matthew Abbott on Oct 19, 2007 at 3:54 PM


When I use this cfc and the ziputility cfc, I get the same errors around the area that the path is used to instantiate the file input stream:

<cfset LOCAL.FileInputStream = CreateObject(
"java",
"java.io.FileInputStream"
).Init(
LOCAL.FilePath
) />

I'm using a full path for both pieces of code:

d:\inetpub\directoryname\anotherdirectoryname\bobthefile.zip

Any clues?

Thanks and happy Friday!
Steph

Posted by Stephanie on Oct 19, 2007 at 6:07 PM


@Stephanie,

If you do this:

#FileExists( "d:\inetpub\directoryname\anotherdirectoryname\bobthefile.zip" )#

... what does it give you?

Posted by Ben Nadel on Oct 19, 2007 at 6:11 PM


It creates the zip drive but it's when it tries to add the files, it chokes. I imagine on filepath.

exact line of zipUtility is 282:

<cfset LOCAL.FileInputStream = CreateObject(
"java",
"java.io.FileInputStream"
).Init(
LOCAL.FilePath
) />

Posted by Stephanie on Oct 19, 2007 at 6:24 PM


Actually sorry that's line 286

Posted by Stephanie on Oct 19, 2007 at 6:25 PM


@Stephanie,

I am not sure what the problem is. Sorry I cannot be more help on this. It looks like you simply cannot create the file stream. The only reason I have come across on this is that the file doesn't exist or is an invalid path or something.

Posted by Ben Nadel on Oct 22, 2007 at 7:29 AM


@Stephanie,

Does the error give you any more information? Usually there is another line with more detail.

Posted by Ben Nadel on Oct 22, 2007 at 7:30 AM


Here is the full error without the full paths for security reasons... The zip file gets created and is 1k but it barfs on adding files to the stream....

Any clues on even how to debug this myself would be great. I am not a java child. I know my CF ok but not java...

Thanks so much!
Steph

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.

Error Occurred While Processing Request
Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: .

The error occurred in ...cfc\ZipUtility.cfc: line 286
Called from ...OpptyImportBackend.cfm: line 186
Called from ...OpptyImportBackend.cfm: line 169
Called from ...OpptyImportBackend.cfm: line 1
Called from ...cfc\ZipUtility.cfc: line 286
Called from ...OpptyImportBackend.cfm: line 186
Called from ...OpptyImportBackend.cfm: line 169
Called from ...OpptyImportBackend.cfm: line 1

284 : "java.io.FileInputStream"
285 : ).Init(
286 : GetFileFromPath(LOCAL.FilePath)
287 : ) />
288 :

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

Resources:
Check the ColdFusion documentation to verify that you are using the correct syntax.
Search the Knowledge Base to find a solution to your problem.


Browser Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 1.0.3705; .NET CLR 2.0.50727; InfoPath.1)
Remote Address 91.103.41.50
Referrer https://.../OpptyImportWizardMapping.cfm
Date/Time 22-Oct-07 09:02 AM

Stack Trace (click to expand)
at cfZipUtility2ecfc1962888858$funcCOMPRESS.runFunction(...cfc\ZipUtility.cfc:286) at cfOpptyImportBackend2ecfm376580135._factor5(...OpptyImportBackend.cfm:186) at cfOpptyImportBackend2ecfm376580135._factor6(...OpptyImportBackend.cfm:169) at cfOpptyImportBackend2ecfm376580135.runPage(...OpptyImportBackend.cfm:1) at cfZipUtility2ecfc1962888858$funcCOMPRESS.runFunction(...cfc\ZipUtility.cfc:286) at cfOpptyImportBackend2ecfm376580135._factor5(...OpptyImportBackend.cfm:186) at cfOpptyImportBackend2ecfm376580135._factor6(...OpptyImportBackend.cfm:169) at cfOpptyImportBackend2ecfm376580135.runPage(...OpptyImportBackend.cfm:1)

java.lang.reflect.InvocationTargetException
at sun.reflect.GeneratedConstructorAccessor274.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:274)
at coldfusion.runtime.java.JavaProxy.CreateObject(JavaProxy.java:128)
at coldfusion.runtime.java.JavaProxy.invoke(JavaProxy.java:56)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:1634)
at cfZipUtility2ecfc1962888858$funcCOMPRESS.runFunction(...cfc\ZipUtility.cfc:286)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:344)
at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:290)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:254)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:56)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:207)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:169)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:194)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:146)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:1634)
at cfOpptyImportBackend2ecfm376580135._factor5(...OpptyImportBackend.cfm:186)
at cfOpptyImportBackend2ecfm376580135._factor6(...OpptyImportBackend.cfm:169)
at cfOpptyImportBackend2ecfm376580135.runPage(...OpptyImportBackend.cfm:1)
at coldfusion.runtime.CfJspPage.invoke(CfJspPage.java:152)
at coldfusion.tagext.lang.IncludeTag.doStartTag(IncludeTag.java:349)
at coldfusion.filter.CfincludeFilter.invoke(CfincludeFilter.java:65)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:225)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:86)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:69)
at coldfusion.filter.BrowserDebugFilter.invoke(BrowserDebugFilter.java:52)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.RequestThrottleFilter.invoke(RequestThrottleFilter.java:115)
at coldfusion.CfmServlet.service(CfmServlet.java:107)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:78)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:257)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:541)
at jrun.servlet.jrpp.JRunProxyService.invokeRunnable(JRunProxyService.java:204)
at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:318)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:426)
at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:264)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
Caused by: java.io.FileNotFoundException: SOBAccreditations200710Mon.xls (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
... 43 more

Posted by Stephanie Spanjian on Oct 22, 2007 at 12:23 PM


I have gotten it to work for ziputility.cfc! I basically have to add entries one by one instead of as a list.

Thanks for the cfc.

Steph

Posted by Stephanie on Oct 22, 2007 at 5:57 PM


What do you mean instead of as a list? How could you do it as a list?

Posted by Ben Nadel on Oct 23, 2007 at 7:24 AM


On page

http://www.bennadel.com/blog/735-ColdFusion-ZipUtility-Component-Can-Now-Write-Directly-To-An-Output-Stream.htm

<cfset objZip.AddFileEntry(
ExpandPath( "./data/file_a.jpg" ),
ExpandPath( "./data/file_b.jpg" )
) />

I have to do this one at a time like so:

<cfset objZip.AddFileEntry(
ExpandPath( "./data/file_a.jpg" )
) />
<cfset objZip.AddFileEntry(
ExpandPath( "./data/file_b.jpg" )
) />

Thanks,
Steph

Posted by Stephanie Spanjian on Oct 23, 2007 at 3:03 PM


Oooh, I see. Ok, that makes sense. Yeah, right now, you have to add one at a time. But, that is a cool idea to do a list.

Posted by Ben Nadel on Oct 23, 2007 at 4:18 PM


Hi Ben,
on some Excel files i get the message Object Instantiation Exception. An exception occurred when instantiating a java object, too.
I tried with expandpath(), but with no success. Now i have downloaded the new version of jarkarta Poi, version 3 on http://apache.secsup.org/dist/jakarta/poi/release/bin/.
I deleted the old poi files files on the coldfusion lib directory, poi-2.5.1-final-20040804.jar, poi-contrib-2.5.1-final-20040804.jar and replaced them with the new version poi-3.0-rc4-20070503.jar and poi-contrib-3.0-rc4-20070503.jar. After restarting the coldfusion server, i can read my Excel files with POIUtility.cfc.

Posted by Felix Kreitner on Oct 31, 2007 at 6:28 PM


I still get the error for the poi function, but not the zip file utility...

Posted by Stephanie Spanjian on Nov 1, 2007 at 2:06 PM


the error:

Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: .

The error occurred in ...\POIUtility.cfc: line 883

............

881 : "org.apache.poi.hssf.usermodel.HSSFWorkbook"
882 : ).Init(
883 : LOCAL.ExcelFileSystem
884 : );
885 :

Posted by Stephanie on Nov 1, 2007 at 2:44 PM


Is it failing because I'm on a windows server and not a unix server?

I wouldn't think that would matter at all but you just never know...

Steph

Posted by Stephanie Spanjian on Nov 1, 2007 at 6:24 PM


@Stephanie,

The system should not matter, as far as I know. I am on a Windows system and it works fine. Can you post the few lines of code that actually creates the POIUtility? ... The CreateObject() lines.

Posted by Ben Nadel on Nov 2, 2007 at 7:19 AM


@Felix,

Interesting. I wonder why it was working in some files and not for others. Maybe it was just a bug in the Java code, seeing as the new release fixed it. Thanks for sharing that the new version works.

Posted by Ben Nadel on Nov 2, 2007 at 7:41 AM


<cfset objPOI = CreateObject("component", "POIUtility" ).Init() >

<cfset arrSheets = objPOI.ReadExcel( FilePath = tempFile , HasHeaderRow = true ) >

<cfoutput>#arrSheets.name#</cfoutput>

It crashes at cfset arrSheets with the error I posted.

Posted by Stephanie on Nov 2, 2007 at 10:33 AM


All I need it for right now is to read in a sheet name.

I also tried the simple code below but it crashes at cfset wb with the same error. I am on a windows server 2003 enterprise edition...

Error:

Object Instantiation Exception.
An exception occurred when instantiating a java object. The cause of this exception was that: .

Code:

<cfset fileIn = createObject("java","java.io.FileInputStream").init("#tempFile#")/>

<cfset fs = createObject("java","org.apache.poi.poifs.filesystem.POIFSFileSystem").init(fileIn)/>

<cfset wb = createObject("java","org.apache.poi.hssf.usermodel.HSSFWorkbook").init(fs)/>
<cfset sheet = wb.getSheetAt(0)/>
<cfoutput>#sheet#</cfoutput>

Any ideas would be helpful:>)

Thanks,
Steph

Posted by Stephanie on Nov 2, 2007 at 10:57 AM


It's the fact that the first row has drop down filtering...

I took that row out and there is no prob, so now I just need to figure out a workaround.

Posted by Stephanie Spanjian on Nov 2, 2007 at 2:06 PM


@Stephanie,

Glad you got it figured out. Sorry that it doesn't handle that right now.

Posted by Ben Nadel on Nov 5, 2007 at 7:30 AM


Ben,

I added the code to handle exporting dates to Excel. Let me know if you want it.

Ed

Posted by Ed Martin on Feb 28, 2008 at 10:18 AM


@Ed,

Heck yeah, I want it :) I will shoot you an email.

Posted by Ben Nadel on Feb 28, 2008 at 10:19 AM


Just to let everyone know, I had the same issue with POI not accepting Excel files where the top row was using filtering, In Excel 2007 (using the old XLS format) I just went to data and unticked "filter" and all worked ok, not sure if filtering is ok on other rows, didn't try it

Posted by Mike on Apr 2, 2008 at 5:56 AM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting