facebooktwittermenuarrow-up

GemBox Support Forum

Obatining values of TableView into 2D array

Hi,

Please I’m quite rusty on JavaFX and I have this problem killing my brain for a couple of days. I am able to load an Excel file into a TableView using the GemBox.Spreadsheet API and can edit those values as well. What I want is to get the entire TableView content into a 2D array of double for further calculations.

This is what I tried something like this which builds fine but on running it I get java.lang.NullPointException error:

double[][] offt = new double[table.getItems().size()][];

for (int row = 0; row < table.getItems().size(); row++) {
    ObservableList cells = (ObservableList) table.getItems().get(row);
    for (int col = 0; col < cells.size(); col++) {
        offt[row][col] = Double.parseDouble(cells.get(col).toString());
    }
}

Hi Austine,

The problem is that arrays inside your offt multidimensional array are not initialized.
You need to either use this:

double[][] offt = new double[table.getItems().size()][];

for (int row = 0; row < table.getItems().size(); row++) {
    ObservableList cells = (ObservableList) table.getItems().get(row);
    offt[row] = new double[cells.size()];
    for (int col = 0; col < cells.size(); col++) {
        offt[row][col] = Double.parseDouble(cells.get(col).toString());
    }
}

Or perhaps something like this:

int rowsCount = table.getItems().size();
int columnsCount = table.getColumns().size();

double[][] offt = new double[rowsCount][columnsCount];

for (int row = 0; row < rowsCount; row++) {
    ObservableList cells = (ObservableList) table.getItems().get(row);
    for (int col = 0; col < columnsCount; col++) {
        offt[row][col] = Double.parseDouble(cells.get(col).toString());
    }
}

Regards,
Mario

1 Like

Thank you mario.it worked.the only thing is it continues to get zeros up to the 26th column which i dont really need.i wish i could be dynamic.thanks once more