0

我正在创建一个将 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)
4

2 回答 2

1

尝试以下操作:

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您调用的 s 之后createExcelSheet,这才是Workbooks 实际被实例化的地方。那crossReferenceButtonListener想在Workbooks 上使用字段,但它们是空的,因此你的NullPointerException.

通过在添加之前调用createExcelSheet方法crossReferenceButtonListener,您将确保在尝试使用它们时log两者sheet都已创建。crossReferenceButtonListener

于 2013-08-23T16:28:28.753 回答
1

Java 没有引用调用语义。您将这两个变量传递给该方法,log显然希望它们在该方法中被初始化,但这不起作用。他们仍然在方法调用之后。sheetcreateExcelSheetnull

createExcelSheet (log,"C:/Log.xlsx", "Log");
createExcelSheet(sheet,"C:/Spreadsheet.xlsx", "Spreadsheet");

您必须重写该方法以返回创建XSSFWorkbook的并将调用更改为

log = createExcelSheet ("C:/Log.xlsx", "Log");
sheet = createExcelSheet("C:/Spreadsheet.xlsx", "Spreadsheet");
于 2013-08-23T16:47:02.310 回答