I am trying to read an excel file into a JTable. I gotten this NoClassDefFoundError at this line:
XSSFWorkbook excelJTableImport = new XSSFWorkbook(excelBIS);
I'm using Apache POI 5.0.0.
Code of table definition:
table = new JTable(); scrollPane.setViewportView(table); table.setModel(new DefaultTableModel( new Object[][] { }, new String[] { "User Name", "Record Date", "Final Score", "Oil Average", "Hydration Average", "Melanin Average", "Oxygen Average", "Redness Average" } ) { Class[] columnTypes = new Class[] { String.class, String.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class, Integer.class }; public Class getColumnClass(int columnIndex) { return columnTypes[columnIndex]; } });
Code when browse button is selected:
JButton btnBrowse = new JButton("Browse File"); btnBrowse.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { //Definitions DefaultTableModel model=null; String userName = ""; String recordDate = ""; Integer finalScore = 0; Integer oilAverage = 0; Integer hydrationAverage = 0; Integer melaninAverage = 0; Integer oxygenAverage = 0; //=========Action here========== File excelFile; FileInputStream excelFIS = null; BufferedInputStream excelBIS = null; XSSFWorkbook workbook=null; //Default Path String defaultPath = "C:\\"; JFileChooser fileChooser=new JFileChooser(defaultPath); int excelChooser=fileChooser.showOpenDialog(null); //Open Button Clicked if(excelChooser==JFileChooser.APPROVE_OPTION) { try { excelFile=fileChooser.getSelectedFile(); excelFIS=new FileInputStream(excelFile); excelBIS=new BufferedInputStream(excelFIS); workbook = new XSSFWorkbook(excelBIS); XSSFSheet excelSheet = workbook.getSheetAt(0); //Loop through excel columns and rows for(int row=0;row<10;row++) { XSSFRow excelRow=excelSheet.getRow(row); XSSFCell excelName = excelRow.getCell(0); XSSFCell excelDate = excelRow.getCell(1); XSSFCell excelScore = excelRow.getCell(2); XSSFCell excelOil = excelRow.getCell(3); XSSFCell excelHydration = excelRow.getCell(4); XSSFCell excelMelanin = excelRow.getCell(5); XSSFCell excelOxygen = excelRow.getCell(6); XSSFCell excelRedness = excelRow.getCell(7); model.addRow(new Object[]{excelName, excelDate, excelScore, excelOil, excelHydration,excelMelanin,excelOxygen,excelRedness}); } }catch(FileNotFoundException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); }catch(IOException ex) { JOptionPane.showMessageDialog(null, ex.getMessage()); }finally { try { if (excelFIS != null) { excelFIS.close(); } if (excelBIS != null) { excelBIS.close(); } if (workbook != null) { workbook.close(); } } catch (IOException iOException) { JOptionPane.showMessageDialog(null, iOException.getMessage()); } } } } });
Additionally, I got a warning at this line
that model can only be null at this location.model.addRow(new Object[]{excelName, excelDate, excelScore, excelOil, excelHydration,excelMelanin,excelOxygen,excelRedness});
What did I do wrong that caused me the warning and error? How do I fix this?
Help is appreciated!