0

I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. My code is as follows:

XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);

    XNameAccess xNamedTables = xTablesSupplier.getTextTables();
    try {
        Object table = xNamedTables.getByName(tblName);
        XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
        XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
        if(flag){
            XColumnRowRange xCollumnAndRowRange =(XColumnRowRange) 
                    UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
                XTableRows rows = xCollumnAndRowRange.getRows();

                 System.out.println("Testing if this works");
                rows.insertByIndex(4, size-4);
        }

I am not sure if I am missing something here or if I should be using a different function.

  • I'm not familiar with Java so not for sure - but in your code, where is the variable `size` getting its value? Does it make any difference if you use the `rows.getCount()` method to obtain the current number of rows? – Lyrl Nov 13 '15 at 14:27
  • The size variable is given prior to this snippet of code. It determines the size of the values which will be inserted (i.e. 5 values or 100 values). the rows value will return null each time as the variable xCollumnAndRowRange is null as well. This is why I thought perhaps the object is incorrect or something else must be used – Danut Niculae Nov 13 '15 at 14:52
  • 1
    What happens if you `XTableRows rows = xTable.getRows()`? – Lyrl Nov 13 '15 at 17:08
  • Thank you Lyrl. That is working indeed. It now returned the right ammount of rows without null pointer. – Danut Niculae Nov 14 '15 at 15:58

1 Answers1

1

As Lyrl suggested, this works:

XTableRows rows = xTable.getRows();

Apparently XColumnRowRange is only used for spreadsheets.

Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface. The code would simply be:

table = tables.getByName(tblName)
rows = table.getRows()
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • Thank you so very much! That was my problem indeed that the XColumnRowRange is used for spreadsheets, which would return a null pointer as i am working with writer document. Now the problem is fixed. – Danut Niculae Nov 14 '15 at 15:57