Código que lee archivo y vuelca los datos:
Código:
La excepción:btnImportCustomer.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Creamos el file chooser JFileChooser fileChooser = new JFileChooser(); // Indico que solo seleccione archivos fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); // Indico los tipos de archivos puede abrir FileNameExtensionFilter filter = new FileNameExtensionFilter("XLS files", "xls"); // Aplico el filtro fileChooser.setFileFilter(filter); // Muestro el dialog int fileSelection = fileChooser.showOpenDialog(jdManageCustomer); //Si el usuario, pincha en aceptar if (fileSelection == JFileChooser.APPROVE_OPTION) { //Seleccionamos el fichero File file = fileChooser.getSelectedFile(); try { // Obtengo la ruta del libro HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(file.getAbsolutePath())); //Se obtiene la primera hoja HSSFSheet sheet = workbook.getSheetAt(0); // Contador de celdas int rowCount = sheet.getLastRowNum(); // Empiezo en 1 para no leer la cabecera del excel for (int i = 1; i < rowCount; i++) { // Creo el cliente Customer customer = new Customer(); customer.setFirstName(sheet.getRow(i).getCell(0).getStringCellValue()); customer.setLastName(sheet.getRow(i).getCell(1).getStringCellValue()); customer.setAddress(sheet.getRow(i).getCell(2).getStringCellValue()); customer.setPostalCode((int) sheet.getRow(i).getCell(3).getNumericCellValue()); customer.setPhone((int) sheet.getRow(i).getCell(4).getNumericCellValue()); customer.setEmail(sheet.getRow(i).getCell(5).getStringCellValue()); CustomerDAO cust = customerFactory.createCustomerDAO(); cust.addCustomer(customer); showCustomerTable(); } // Cierro el libro workbook.close(); } catch (IOException | SQLException ex) { JOptionPane.showMessageDialog(null, "Error de lectura, eliga archivo correcto"); } } } }); }
Código:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.maurifuentes.sm.view.ManageCustomerView$10.actionPerformed(ManageCustomerView.java:549) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Exception while removing reference.