-2

my code is read a excel file and store data in two dimensional String array through dynamically initialization and then print that String array when i run this code java.lang.NullPointerException is raised . if i ininitialization array in static method it work good but i want array initialization in dynamically

excel file age place 32 chennai 31 new york 55 indiana 12 lohoas 13 mumbai 111 pune 45 los angel

java code

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    JFileChooser open = new JFileChooser();
    open.showOpenDialog(null);
    File f = open.getSelectedFile();
    String filepath = f.getAbsolutePath();
    jTextField1.setText(filepath);
    try {
        Workbook wb = Workbook.getWorkbook(f);
        Sheet sh = wb.getSheet(0);
        int row = sh.getRows();
        int col = sh.getColumns();

        String[][] ss = null;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j <= col; j++) {

                Cell c2 = sh.getCell(j, i);
                System.out.print(c2.getContents() + "\t");
                ss[j][i] = c2.getContents();

            }
            System.out.print("\n");
        }

        System.out.println("array");
        for (int i = 0; i < ss.length; i++) {
            for (int j = 0; j < ss.length; j++) {
                System.out.print(ss[i][j] + "\t");
            }
        }

    } catch (IOException ex) {
        Logger.getLogger(rdwr.class.getName()).log(Level.SEVERE, null, ex);
    } catch (BiffException ex) {
        Logger.getLogger(rdwr.class.getName()).log(Level.SEVERE, null, ex);
    }

}

  

1 Answers1

0

I think in your inner for loop of col you have use <= which is causing an issue.

replace this

for (int j = 0; j <= col; j++) {

with

for (int j = 0; j < col; j++) {
Vivek Jain
  • 2,730
  • 6
  • 12
  • 27