This Question is Not Answered

1 "correct" answer available (4 pts) 2 "helpful" answers available (2 pts)
4 Replies Last post: Mar 1, 2007 3:32 AM by Alejandro  
Alejandro   20 posts since
Jan 12, 2007
Currently Being Moderated

Jan 30, 2007 4:38 AM

help improving stability to read values from Excel file (web testing)

Hello,

 

I'm having some issues reading values from an opened excel file (through web testing).  I only have one class which I think can be responsible for this- which I hope to later parametrize to take expected values, etc.  Strangely, the code doesn't work for getting an already running instance of Excel.  I later added some code (which was probably silly) to open a new instance of excel first, then to grab the already opened instance...and that seemed to work.  However, the code always crashes on first attempt when I reboot my computer.  Then it mostly works.  With a random failure here and there.  It is about 90% effective.  Can you perhaps give me some pointers or suggestions on what I may be doing wrong?  I'll attach my class file, and then the 2 stack traces (1st trace = with trying 2 instances of Excel, which mostly works, 2nd trace = by just grabbing the active instance which never works).  Thank you very much for your time, 

 

-Alejandro

 

-


stack trace of the class as is:

 

java.lang.reflect.InvocationTargetException

     at com.jniwrapper.win32.MessageLoopThread.doInvokeAndWait(MessageLoopThread.java:232)

     at com.jniwrapper.win32.MessageLoopThread.doInvokeMethod(MessageLoopThread.java:296)

     at com.jniwrapper.win32.automation.OleMessageLoop.invokeMethod(SourceFile:191)

     at lib.ExcelCommunicator2.testRow(ExcelCommunicator2.java:40)

     at watij.WatijBatchTemplateReport.testTrailBlaze(WatijBatchTemplateReport.java:89)

     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

     at java.lang.reflect.Method.invoke(Unknown Source)

     at junit.framework.TestCase.runTest(TestCase.java:154)

     at junit.framework.TestCase.runBare(TestCase.java:127)

     at junit.framework.TestResult$1.protect(TestResult.java:106)

     at junit.framework.TestResult.runProtected(TestResult.java:124)

     at junit.framework.TestResult.run(TestResult.java:109)

     at junit.framework.TestCase.run(TestCase.java:118)

     at junit.framework.TestSuite.runTest(TestSuite.java:208)

     at junit.framework.TestSuite.run(TestSuite.java:203)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Caused by: java.lang.reflect.InvocationTargetException

     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

     at java.lang.reflect.Method.invoke(Unknown Source)

     at com.jniwrapper.win32.MessageLoopThread$MethodInvocationAction.run(MessageLoopThread.java:656)

     at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:502)

Caused by: java.lang.NullPointerException

     at lib.ExcelCommunicator2.getProperty(ExcelCommunicator2.java:122)

     at lib.ExcelCommunicator2.testValue(ExcelCommunicator2.java:68)

     ... 6 more

 

-


stack trace without using the "pre" method  (never works):

 

java.lang.reflect.InvocationTargetException

     at com.jniwrapper.win32.MessageLoopThread.doInvokeAndWait(MessageLoopThread.java:232)

     at com.jniwrapper.win32.MessageLoopThread.doInvokeMethod(MessageLoopThread.java:296)

     at com.jniwrapper.win32.automation.OleMessageLoop.invokeMethod(SourceFile:191)

     at lib.ExcelCommunicator2.testRow(ExcelCommunicator2.java:37)

     at watij.WatijBatchTemplateReport.testTrailBlaze(WatijBatchTemplateReport.java:89)

     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

     at java.lang.reflect.Method.invoke(Unknown Source)

     at junit.framework.TestCase.runTest(TestCase.java:154)

     at junit.framework.TestCase.runBare(TestCase.java:127)

     at junit.framework.TestResult$1.protect(TestResult.java:106)

     at junit.framework.TestResult.runProtected(TestResult.java:124)

     at junit.framework.TestResult.run(TestResult.java:109)

     at junit.framework.TestCase.run(TestCase.java:118)

     at junit.framework.TestSuite.runTest(TestSuite.java:208)

     at junit.framework.TestSuite.run(TestSuite.java:203)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)

     at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

Caused by: java.lang.reflect.InvocationTargetException

     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

     at java.lang.reflect.Method.invoke(Unknown Source)

     at com.jniwrapper.win32.MessageLoopThread$MethodInvocationAction.run(MessageLoopThread.java:656)

     at com.jniwrapper.win32.MessageLoopThread$LoopThread.run(MessageLoopThread.java:502)

Caused by: com.jniwrapper.win32.com.ComException: COM object method returns error code: 0x800401E3; Operation unavailable

     at com.jniwrapper.win32.com.ComException.checkResult(SourceFile:72)

     at com.jniwrapper.win32.com.ComFunctions.getActiveObject(SourceFile:414)

     at lib.ExcelCommunicator2.init(ExcelCommunicator2.java:58)

     ... 6 more

Attachments:
Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
1. Feb 2, 2007 11:38 AM in response to: Alejandro
Re: help improving stability to read values from Excel file (web testing)

Hi Alejandro,

 

I have several comments and questions about your code:

 

- It's not necessary to initialize COM in "pre()" and "init()" methods using the ComFunctions.coInitialize(), since COM gets initialized in OleMessageLoop thread automatically.

 

-Why do you get these two separate methods at all? I think you can simply remove that "pre()" method and leave just "init()" method:

 

  


    staic final CLSID CLSID_MSExcelApplication = new CLSID(classID);
    public void init() {
        application = new IDispatchImpl(CLSID_MSExcelApplication, ClsCtx.LOCAL_SERVER);
        application.setAutoDelete(false);
    }
   

 

This method always create a dedicated MS Excel server and does not relay on the currently running instance.

 

- Also, I am wondering about the implementation of the getProperty() method. In your example it's declared as following:

 

   

 
    private static IDispatch getProperty(IDispatch obj, String propertyName) {
        Automation methodAutomation = new Automation(obj);
        try {
            IDispatchImpl pdispVal = (IDispatchImpl)methodAutomation.getProperty(propertyName).getPdispVal();
            pdispVal.setAutoDelete(false);
            return pdispVal;
        } finally {
             methodAutomation.release();
        }
    }
    

 

Actually this code can release the 'pdispVal' object (if its refCount was 1) just before it is returned from the method. I think you need to simplify that code and leave the default resource management as is:

 

   

 
    private static IDispatch getProperty(IDispatch obj, String propertyName) {
        Automation methodAutomation = new Automation(obj);
        return (IDispatchImpl)methodAutomation.getProperty(propertyName).getPdispVal();
    }
    

 

 

Now a couple words about that stack traces. In first case the cause was in NPE in your code. In second case it failed because of "0x800401E3; Operation unavailable" error, which means that ComFunctions.getActiveObject() function was unable to find the specified COM server running in the system.

 

After all, I suggest you to take a look on our JExcel library where all these issues are solved more or less: http://www.teamdev.com/jexcel/index.jsf

 

-Serge

Serge Piletsky TeamDev Ltd. 670 posts since
Apr 24, 2006
Currently Being Moderated
3. Feb 6, 2007 1:25 PM in response to: Alejandro
Re: help improving stability to read values from Excel file (web testing)

Hi Alejandro,

 

As I already mentioned, that error (0x800401E3; Operation unavailable) is only thrown when there is no MS Excel application running in the system.

 

In other words, this "ComFunctions.getActiveObject(CLSID_MSExcelApplication);" method allows you to connect to a running application, but it does not start application itself. Also, this method does not allow to get the required instance of application, because there is no such ability to choose the one in COM. So, if you have multiple instances of MS Excel that are running in the system, you will get only the first started one, if I am not mistaking.

 

-Serge

More Like This

  • Retrieving data ...