Apache OpenOffice (AOO) Bugzilla – Issue 99688
CastCastException for example code from the DevGuide
Last modified: 2013-02-07 22:37:11 UTC
When copying and pasting the following code from the current DevGuide into my project I got a ClassCastException: public com.sun.star.sheet.XSpreadsheet getSpreadsheet( com.sun.star.sheet.XSpreadsheetDocument xDocument, int nIndex) { // Collection of sheets com.sun.star.sheet.XSpreadsheets xSheets = xDocument.getSheets(); com.sun.star.sheet.XSpreadsheet xSheet = null; try { com.sun.star.container.XIndexAccess xSheetsIA = (com.sun.star.container.XIndexAccess) UnoRuntime.queryInterface(com.sun.star.container.XIndexAccess.class, xSheets); 226: xSheet = (com.sun.star.sheet.XSpreadsheet) xSheetsIA.getByIndex(nIndex); } catch (Exception ex) { } return xSheet; } The exception being thrown is: java.lang.ClassCastException: com.sun.star.uno.Any at getSpreadsheet(MyClass.java:226) Instead of xSheet = (com.sun.star.sheet.XSpreadsheet) xSheetsIA.getByIndex(nIndex); xSheet = (com.sun.star.sheet.XSpreadsheet) xSheetsNA.getByName(aName); the correct code must be: xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsNA.getByName(aName)); xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, xSheetsIA.getByIndex(nIndex));