Issue 99688 - CastCastException for example code from the DevGuide
Summary: CastCastException for example code from the DevGuide
Status: UNCONFIRMED
Alias: None
Product: documentation
Classification: Unclassified
Component: Manuals (show other issues)
Version: OOo 3.0.1
Hardware: Unknown All
: P3 Trivial (vote)
Target Milestone: ---
Assignee: AOO issues mailing list
QA Contact:
URL: http://api.openoffice.org/docs/Develo...
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-26 21:14 UTC by mirumpf
Modified: 2013-02-07 22:37 UTC (History)
1 user (show)

See Also:
Issue Type: DEFECT
Latest Confirmation in: ---
Developer Difficulty: ---


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description mirumpf 2009-02-26 21:14:53 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));