gpt4 book ai didi

java - 程序执行 POI、JAVA 时从 Excel 电子表格读取插入的数据

转载 作者:行者123 更新时间:2023-12-02 06:47:49 27 4
gpt4 key购买 nike

我正在创建一个应用程序,它将 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)

最佳答案

尝试以下操作:

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);

// NOTE the order below has been flipped!!!!
createExcelSheet (log,"C:/Log.xlsx", "Log");
createExcelSheet(sheet,"C:/Spreadsheet.xlsx", "Spreadsheet");

crossReference.addActionListener(new crossReferenceButtonListener());
generateHPD.addActionListener(new generateHPDButtonListener());
}

在原始代码中,您创建按钮,然后添加操作监听器:

crossReference.addActionListener(new crossReferenceButtonListener());

但是,只有在添加 crossReferenceButtonListener 后,您才能调用 createExcelSheet,这是实际实例化 Workbook 的地方。 crossReferenceButtonListener 想要使用 Workbook 上的字段,但它们为空,因此出现 NullPointerException

通过在添加 crossReferenceButtonListener 之前调用 createExcelSheet 方法,您将确保 logsheetcrossReferenceButtonListener 尝试使用“em”时已创建。

关于java - 程序执行 POI、JAVA 时从 Excel 电子表格读取插入的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18405673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com