我正在创建一个将 excel 文件读入程序并对其进行操作的应用程序。在此之前,一切正常。但是当我开始为我的应用程序实现 GUI 时,它不再工作了。
这是代码:
当程序启动时,它会创建一个带有按钮的 gui 并打开 2 个新的电子表格。我在这些电子表格中输入数据,然后按下调用 crossReferenceButtonListener 的按钮,但是它无法将数据取出。下面的错误信息
public GUI () throws Exception{
this.setTitle("NYWM Cross Reference Application");
this.setSize(400,100);
this.setVisible(true);
this.setLocationRelativeTo(null);
buttonHolder = new Panel (new BorderLayout());
this.add(buttonHolder);
crossReference = new Button ("CrossReference");
generateHPD = new Button ("Generate HPD");
buttonHolder.add(crossReference, BorderLayout.NORTH);
buttonHolder.add(generateHPD, BorderLayout.SOUTH);
crossReference.addActionListener(new crossReferenceButtonListener());
generateHPD.addActionListener(new generateHPDButtonListener());
createExcelSheet (log,"C:/Log.xlsx", "Log");
createExcelSheet(sheet,"C:/Spreadsheet.xlsx", "Spreadsheet");
}
private void createExcelSheet (XSSFWorkbook wb, String path, String fileName) throws Exception{
try{
FileOutputStream out = new FileOutputStream (path);
wb = new XSSFWorkbook ();
XSSFSheet sheet = wb.createSheet(fileName);
wb.write(out);
Process p = Runtime.getRuntime().exec(
"rundll32 url.dll, FileProtocolHandler " + path);
}
catch (Exception e){
throw e;
}
}
private class crossReferenceButtonListener implements ActionListener {
public void actionPerformed (ActionEvent event){
try {
ref = new CrossReference (log.getSheet("Log"), sheet.getSheet("Spreadsheet"));
ref.CrossReference();
JOptionPane.showMessageDialog(null, "Cross Reference was successful!");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
错误消息:更具体地说,它在这一行失败
ref = new CrossReference (log.getSheet("Log"), sheet.getSheet("Spreadsheet"));
错误:
at java.awt.EventDispatchThread.run(Unknown Source)
java.lang.NullPointerException
at nywm_crossReferenceApplication.GUI$crossReferenceButtonListener.actionPerformed(GUI.java:70)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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)