我正在尝试在Excel工作表中加载JTable
值,但它给出了空指针异常。现在,当我尝试在j的for循环后使用break插入数据时,会将其插入excel,而Other则将其提供Null Pointer Exception。
下面是代码:
private void writeToExcel() {
XSSFWorkbook ws = new XSSFWorkbook();
XSSFSheet wb = ws.createSheet();
XSSFRow row = wb.createRow(0);;
XSSFCell cell = null;
for (int i = 0; i < datatypetable.getRowCount(); i++) {
int ColNum = 0;
row = wb.createRow(i);
for (int j = 0; j < datatypetable.getColumnCount(); j++) {
cell = wb.getRow(i).getCell(ColNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell == null) {
System.out.println("Cell Project is: " + cell.toString());
cell = row.createCell(ColNum);
cell.setCellValue(datatypetable.getValueAt(i, j).toString());
} else {
//cell = row.createCell(j);
System.out.println("Cell Object is: " + cell.toString());
System.out.println(datatypetable.getValueAt(i, j).toString());
**cell.setCellValue(datatypetable.getValueAt(i, j).toString());**
}
ColNum++;
}
}
//Write Excel to file System
try {
FileOutputStream fos = new FileOutputStream(workingDir + File.separator + "First" + ".xls");
ws.write(fos);
fos.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(DataEntryArea.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DataEntryArea.class.getName()).log(Level.SEVERE, null, ex);
}
}
代码中的错误位于 cell.setCellValue(datatypetable.getValueAt(i,j).toString()); -
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.boolment.dataentry.DataEntryArea.writeToExcel(DataEntryArea.java:1243)
at com.boolment.dataentry.DataEntryArea.jButton6ActionPerformed(DataEntryArea.java:1263)
at com.boolment.dataentry.DataEntryArea.access$2000(DataEntryArea.java:87)
at com.boolment.dataentry.DataEntryArea$23.actionPerformed(DataEntryArea.java:717)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我知道此代码有一个解决方案,我也尝试过这样做,但仍然无法正常工作。我该如何解决呢?
答案 0 :(得分:0)
我已经找到了获取空指针异常的解决方案。由于此datatypetable.getValueAt(i, j).toString()
,我收到了错误信息,因为table的某些值可能为null,而当它们为null时,它会收到错误NullPointerException。
for (int i = 0; i < datatypetable.getRowCount(); i++) {
int ColNum = 0;
row = wb.createRow(i);
for (int j = 0; j < datatypetable.getColumnCount(); j++) {
cell = wb.getRow(i).getCell(ColNum, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
String item1 = null;
if (datatypetable.getValueAt(i, j) != null) {
cell.setCellValue(datatypetable.getValueAt(i, j).toString());
} else {
cell.setCellValue(item1);
}
ColNum++;
}
}
我没有提供完整的代码,因为上述代码与我要求的相同。唯一的更改是在此for循环中进行的。 希望我的回答对任何人有帮助。