当前位置: 首页 > 知识库问答 >
问题:

Swing GUI with IntelliJ error“contentPane不能设置为null。”从终端编译时

柯昱
2023-03-14

当我从IntelliJ编译我的项目时,没有任何问题,我的程序运行顺利,但是当我尝试使用javac从终端编译它时,警告“注意:Victor/演示文稿/TableControlllerMenu.java使用未经检查或不安全的操作。”弹出,当我尝试执行它时,我得到错误“线程中的异常”main“java.awt.IllegalComponentStateException: contentPane不能设置为null。”。我猜IntelliJ没有在文件中编写对象contentPane的初始化代码,这导致javac抓狂。我尝试更改选项File-

这是GUI的代码,如果需要更多代码,请告诉我:

package Victor.presentation;

import Victor.domain.TCPresPersInterface;
import Victor.persistence.TCRow;
import Victor.persistence.TCUtilities;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class TableControllerMenu extends JFrame {
    static private TCPresPersInterface persInterface;
    private DefaultTableModel newModel;
    private JTableHeader newHeader;

    private JPanel mainPanel;
    private JButton rowEditorButton;
    private JButton columnCastingButton;
    private JButton manageTablesButton;
    private JButton columnEditorButton;
    private JButton basicUtilitiesButton;
    private JButton fileHandlerButton;
    private JButton goBackButton;
    private JPanel ManageTablesPanel;
    private JPanel BasicUtilitiesPanel;
    private JPanel RowEditorPanel;
    private JPanel ColumnEditorPanel;
    private JPanel ColumnCastingPanel;
    private JPanel FileHandlerPanel;
    private JButton duplicateCurrentTableButton;
    private JButton renameCurrentTableButton;
    private JTextField duplicateTableNameField;
    private JTextField renameTableField;
    private JButton renameColumnButton;
    private JButton modifyItemButton;
    private JButton clearTableButton;
    private JTextField newColumnNameField;
    private JTextField newItemField;
    private JTextField selectedColumnRenameField;
    private JTextField newItemColumnField;
    private JTextField newItemRowField;
    private JCheckBox clearTableConfirmationBox;
    private JButton addEmptyRowButton;
    private JButton duplicateRowButton;
    private JButton deleteRowButton;
    private JTextField emptyRowField;
    private JTextField rowDuplicateField;
    private JTextField positionDuplicatedField;
    private JTextField rowDeleteFIeld;
    private JButton addEmptyColumnButton;
    private JButton duplicateColumnButton;
    private JButton moveColumnButton;
    private JButton negateBinaryColumnButton;
    private JButton deleteColumnButton;
    private JTextField addEmptyColPosFIeld;
    private JTextField duplicateColColFIeld;
    private JTextField duplicatedColPosField;
    private JTextField moveColColFIeld;
    private JTextField moveColPosFIeld;
    private JTextField negateColColField;
    private JTextField deleteColColField;
    private JButton columnToNumericalButton;
    private JButton columnToBinaryButton;
    private JButton columnToBinaryByButton;
    private JButton columnToCategoricalButton;
    private JTextField colNumericalColField;
    private JTextField colBinaryColField;
    private JComboBox colBinItemsBox;
    private JTextField colBinIntervalDivField;
    private JComboBox colBinaryIntervalLowerBox;
    private JComboBox colBinEqualBox;
    private JTextField colCategoricalField;
    private JComboBox selItemsValBox;
    private JTextField colBinIntervalColField;
    private JButton importCSVButton;
    private JButton exportCSVButton;
    private JTextField importPathField;
    private JTextField pathExportField;
    private JTextField separatorImportField;
    private JComboBox headerImportField;
    private JComboBox dataTypeImportBox;
    private JTextField separatorExportField;
    private JComboBox headerExportField;
    private JComboBox dataTypeExportField;
    private JTabbedPane currentMenuTab;
    private JPanel currentMenuPane;
    private JTabbedPane tabbedPane1;
    private JPanel currentTablePanel;
    private JTable currentTable;
    private JComboBox newColumnDType;
    private JLabel manageTablesErrorLog;
    private JLabel currentTableText;
    private JLabel tableSizeText;
    private JLabel errorLogText;
    private JLabel tableHeaderText;
    private JLabel tableTypesText;
    private JTextField separatorColBinFIeld;
    private JTextField itemsColBinFIeld;
    private JButton deleteItemBoxButton;

    private TableControllerMenu(String appName) {
        super(appName);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setContentPane(mainPanel);
        this.pack();

        persInterface = TCPresPersInterface.getInstance();

        actualize(true);

        //      DUPLICATE CURRENT TABLE BUTTON
        duplicateCurrentTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String newName = duplicateTableNameField.getText();

                String error = persInterface.duplicateTable(newName);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    duplicateTableNameField.setText("");
                }
            }
        });

        //      RENAME CURRENT TABLE BUTTON
        renameCurrentTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {

                String newName = renameTableField.getText();

                String error = persInterface.renameTable(newName);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(false);
                    renameTableField.setText("");
                }
            }
        });

        //      IMPORT TABLE FROM FILE BUTTON
        importCSVButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String path = importPathField.getText();
                String separator = separatorImportField.getText();
                String header = (String) headerImportField.getSelectedItem();
                String type = (String) dataTypeImportBox.getSelectedItem();

                String error = persInterface.fileHandling(false, path, separator, header, type);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    importPathField.setText("");
                    separatorImportField.setText("");
                    headerImportField.setSelectedIndex(0);
                    dataTypeImportBox.setSelectedIndex(0);
                }
            }
        });

        //      EXPORT TABLE TO FIELD BUTTON
        exportCSVButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String path = pathExportField.getText();
                String separator = separatorExportField.getText();
                String header = (String) headerExportField.getSelectedItem();
                String type = (String) dataTypeExportField.getSelectedItem();

                String error = persInterface.fileHandling(true, path, separator, header, type);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(false);
                    pathExportField.setText("");
                    separatorExportField.setText("");
                    headerExportField.setSelectedIndex(0);
                    dataTypeExportField.setSelectedIndex(0);
                }
            }
        });

        //      RENAME COLUMN BUTTON
        renameColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String newName = newColumnNameField.getText();
                String col = selectedColumnRenameField.getText();

                String error = persInterface.renameColumn(newName, col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    newColumnNameField.setText("");
                    selectedColumnRenameField.setText("");
                }
            }
        });

        //      MODIFY ITEM BUTTON
        modifyItemButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String value = newItemField.getText();
                String row = newItemRowField.getText();
                String col = newItemColumnField.getText();

                String error = persInterface.modifyItem(value, row, col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    newItemField.setText("");
                    newItemRowField.setText("");
                    newItemColumnField.setText("");
                }
            }
        });

        //      CLEAR TABLE BUTTON
        clearTableButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                if (clearTableConfirmationBox.isSelected()) {
                    String error = persInterface.clearTable();

                    if (!error.equals(""))
                        errorLogText.setText("ERROR: " + error);
                    else {
                        errorLogText.setText("");
                        actualize(true);
                        clearTableConfirmationBox.setSelected(false);
                    }
                } else
                    errorLogText.setText("ERROR: the 'are you sure' box must be marked in order to clear the table");
            }
        });

        //      ADD EMPTY ROW BUTTON
        addEmptyRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String position = emptyRowField.getText();

                String error = persInterface.addEmptyRow(position);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    emptyRowField.setText("");
                }
            }
        });

        //      DUPLICATE ROW BUTTON
        duplicateRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String row = rowDuplicateField.getText();
                String pos = positionDuplicatedField.getText();

                String error = persInterface.duplicateRow(row, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    rowDuplicateField.setText("");
                    positionDuplicatedField.setText("");
                }
            }
        });

        //      DELETE ROW BUTTON
        deleteRowButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String row = rowDeleteFIeld.getText();

                String error = persInterface.deleteRow(row);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    rowDeleteFIeld.setText("");
                }
            }
        });

        //      ADD EMPTY COLUMN BUTTON
        addEmptyColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String pos = addEmptyColPosFIeld.getText();
                String dtype = (String) newColumnDType.getSelectedItem();

                String error = persInterface.addEmptyColumn(pos, dtype);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    addEmptyColPosFIeld.setText("");
                    newColumnDType.setSelectedIndex(0);
                }
            }
        });

        //      DUPLICATE COLUMN BUTTON
        duplicateColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = duplicateColColFIeld.getText();
                String pos = duplicatedColPosField.getText();

                String error = persInterface.duplicateCol(col, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    duplicateColColFIeld.setText("");
                    duplicatedColPosField.setText("");
                }
            }
        });

        //      MOVE COLUMN BUTTON
        moveColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = moveColColFIeld.getText();
                String pos = moveColPosFIeld.getText();

                String error = persInterface.moveCol(col, pos);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    moveColColFIeld.setText("");
                    moveColPosFIeld.setText("");
                }
            }
        });

        //      NEGATE BINARY COLUMN BUTTON
        negateBinaryColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = negateColColField.getText();

                String error = persInterface.negateBinaryCol(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    negateColColField.setText("");
                }
            }
        });

        //      DELETE COLUMN BUTTON
        deleteColumnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = deleteColColField.getText();

                String error = persInterface.deleteCol(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    deleteColColField.setText("");
                }
            }
        });

        //      COLUMN TO NUMERICAL BUTTON
        columnToNumericalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = colNumericalColField.getText();

                String error = persInterface.colToNumeric(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colNumericalColField.setText("");
                }
            }
        });

        //      COLUMN TO BINARY BUTTON
        columnToBinaryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String items = itemsColBinFIeld.getText();
                String separator = separatorColBinFIeld.getText();
                String castTo = (String) selItemsValBox.getSelectedItem();
                String col = colBinaryColField.getText();

                String error = persInterface.colToBinary(col, items, separator, castTo);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    selItemsValBox.setSelectedIndex(0);
                    colBinaryColField.setText("");
                    itemsColBinFIeld.setText("");
                    separatorColBinFIeld.setText("");
                }
            }
        });

        //      COL TO BINARY INTERVAL
        columnToBinaryByButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String div = colBinIntervalDivField.getText();
                String lower = (String) colBinaryIntervalLowerBox.getSelectedItem();
                String equal = (String) colBinEqualBox.getSelectedItem();
                String col = colBinIntervalColField.getText();

                String error = persInterface.colToBinaryInterval(col, div, lower, equal);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colBinIntervalDivField.setText("");
                    colBinaryIntervalLowerBox.setSelectedIndex(0);
                    colBinEqualBox.setSelectedIndex(0);
                    colBinIntervalColField.setText("");
                }
            }
        });

        //      COLUMN TO CATEGORICAL BUTTON
        columnToCategoricalButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String col = colCategoricalField.getText();

                String error = persInterface.colToCategory(col);

                if (!error.equals(""))
                    errorLogText.setText("ERROR: " + error);
                else {
                    errorLogText.setText("");
                    actualize(true);
                    colCategoricalField.setText("");
                }
            }
        });
    }

    private void actualize(Boolean reloadTable) {
        String current = persInterface.getCurrentTable();
        currentTableText.setText("Current table: " + current);
        tableSizeText.setText("Size: " + persInterface.getSizeText());
        errorLogText.setText("");

        if (reloadTable) {
            try {
                //TCUtilities.print(current);

                int n_cols = TCUtilities.getNCols(current);
                int n_rows = TCUtilities.getNRows(current);

                String header_string = Arrays.toString(TCUtilities.getHeader(current).toArray());
                header_string = header_string.replace("[", "");
                header_string = header_string.replace("]", "");
                String type_string = "";
                for (int i = 0; i < n_cols; i++) {
                    if (i != 0) type_string += ", ";
                    type_string += TCUtilities.getColType(current, i).toString();
                }

                tableHeaderText.setText("Header: " + header_string);
                tableTypesText.setText("Column types: " + type_string);

                newModel = new DefaultTableModel(n_cols, n_rows + 2);

                for (int i = 0; i < TCUtilities.getNRows(current); i++) {
                    newModel.insertRow(i, (Object[]) TCRow.getRow(current, i).toArray(new Object[n_cols]));
                }

                currentTable.setModel(newModel);

                newHeader = new JTableHeader();

            } catch (Exception e) {
                System.out.println("ERROR IN ACTUALIZE: " + e.getMessage());
            }
        }
    }

    public static String execute(String currentTable) {
        TCPresPersInterface.setCurrentTable(currentTable);

        JFrame frame = new TableControllerMenu("Table Managing and Editing");
        frame.setVisible(true);

        return TCPresPersInterface.getCurrentTable();
    }

    public static void setCurrentTable(String currentTable) {
        persInterface.setCurrentTable(currentTable);
    }

    public static Boolean hasCurrentTable() {
        return persInterface.hasCurrentTable();
    }

}

共有1个答案

辛弘壮
2023-03-14

我终于解决了。我做的是:文件-

 类似资料:
  • 我正在试用intellij,并且正在尝试进行一些swing开发。我遇到了一个在eclipse上从未遇到过的问题,我想知道我是否设置错了什么。 下面是我的驱动程序运行的GUI类: } 据我所知,我的run()方法非常简单。但是,在编译时,我收到的错误是: 无论出于什么原因,intellij自动创建的代码没有正确初始化JPanel,这就是它为空的原因。 我尝试在run内部实例化它(panel=new

  • 使用Spring3设置hibernate有更多问题。这一次是说连接是nul,因为没有设置方言,它在我的hibernate.cfg.xml文件上。

  • 问题内容: 因此,如果我有一个,由于某种原因,如果我在另一段代码(如)中引用它,则在编译过程中它不会内联到代码中。因此,它不是在被编译之后而是。 问题答案: 您不是编译时间常数,因为JLS 表示 不是。只能在常量表达式中使用的类型是基本类型和。 它的意义是,一个实例(通常)具有语义上重要的对象标识,该标识将其与其他实例区分开。此对象标识不能编码在类文件中……至少,不能用当前的类文件格式编码。(如果

  • 我有一个简单的测试设置,如 但当我尝试编译测试时,我会遇到53个错误,比如 实际上并没有传达任何关于问题所在的有用信息。我只能假设在我的构建中没有正确配置某些内容。sbt文件或其他地方。 这段代码确实曾经工作过,在我清理东西的过程中,事情发生了变化,现在它被破坏了,没有好的诊断。 有人能提出要找的东西吗?

  • 我已经下载了Xcode 8.0测试版,其中包括更新的Swift 3编译器。是否可以在终端上使用Swift 3编译器? 例如,我可以使用“Swift(filename)”命令在终端中编译和运行Swift 2.2代码。