0

我试图自己解决这个问题,但我不知道如何解决这个问题。

当我尝试运行 addressBookUi 类时出现此错误。

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at addressBookUI.startComponents(addressBookUI.java:43)
at addressBookUI.<init>(addressBookUI.java:6)
at addressBookUI$2.run(addressBookUI.java:143)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(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)

这是我的地址BookUi类

    import javax.swing.table.DefaultTableModel;

public class addressBookUI extends javax.swing.JFrame {

    public addressBookUI() {
        startComponents();
    }

    private void startComponents() {
        mainScrollPane = new javax.swing.JScrollPane();
        mainTable = new javax.swing.JTable();
        addButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        mainMenuBar = new javax.swing.JMenuBar();
        mainMenu = new javax.swing.JMenu();
        newMenuItem = new javax.swing.JMenuItem();
        openMenuItem = new javax.swing.JMenuItem();
        saveMenuItem = new javax.swing.JMenuItem();
        printMenuItem = new javax.swing.JMenuItem();
        quitMenuItem = new javax.swing.JMenuItem();
        editMenuBar = new javax.swing.JMenu();
        editPersonsMenuItem = new javax.swing.JMenuItem();
        sortNameMenuItem = new javax.swing.JMenuItem();
        sortPostcodeMenuItem = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        mainTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null },
                        { null, null, null, null }, { null, null, null, null },
                        { null, null, null, null } }, new String[] { "Title 1",
                        "Title 2", "Title 3", "Title 4" }));

        mainScrollPane.setViewportView(mainTable);

        addButton.setText("Add");

        saveButton.setText("Save Row");

        deleteButton.setText("Delete Row");

        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        mainMenu.setText("File");

        newMenuItem.setText("New");
        mainMenu.add(newMenuItem);

        openMenuItem.setText("Open");

        saveMenuItem.setText("Save");
        mainMenu.add(saveMenuItem);

        printMenuItem.setText("Print");
        mainMenu.add(printMenuItem);

        quitMenuItem.setText("Quit");
        mainMenu.add(quitMenuItem);

        mainMenuBar.add(mainMenu);

        editMenuBar.setText("Edit");

        editPersonsMenuItem.setText("Edit Selected Persons");
        editMenuBar.add(editPersonsMenuItem);

        sortNameMenuItem.setText("Sort by Name");
        editMenuBar.add(sortNameMenuItem);

        sortPostcodeMenuItem.setText("Sort by Postcode");
        editMenuBar.add(sortPostcodeMenuItem);

        mainMenuBar.add(editMenuBar);

//      mainMenuBar.add(helpMenuBar);

        setJMenuBar(mainMenuBar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(mainScrollPane)
                .addGroup(
                        layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(addButton,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        155,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(saveButton,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        155,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(deleteButton,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        155,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(refreshButton,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        155,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap(51, Short.MAX_VALUE)));
        layout.setVerticalGroup(layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(
                        layout.createSequentialGroup()
                                .addComponent(mainScrollPane,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        163,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(
                                        layout.createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(addButton)
                                                .addComponent(saveButton)
                                                .addComponent(deleteButton)
                                                .addComponent(refreshButton))
                                .addGap(0, 17, Short.MAX_VALUE)));

        pack();
    }

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_refreshButtonActionPerformed

    }

    public static void main(String[] args) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addressBookUI().setVisible(true);
            }
        });

    }

//  protected javax.swing.JMenuItem aboutHelpMenuBar;
    protected javax.swing.JButton addButton;
    protected javax.swing.JButton deleteButton;
    private javax.swing.JMenu editMenuBar;
    protected javax.swing.JMenuItem editPersonsMenuItem;
    private javax.swing.JMenu mainMenu;
//  private javax.swing.JMenu helpMenuBar;
    private javax.swing.JScrollPane mainScrollPane;
    private javax.swing.JMenuBar mainMenuBar;
    protected javax.swing.JTable mainTable;
    protected javax.swing.JMenuItem newMenuItem;
    protected javax.swing.JMenuItem openMenuItem;
    protected javax.swing.JMenuItem printMenuItem;
    protected javax.swing.JMenuItem quitMenuItem;
    protected javax.swing.JButton refreshButton;
    protected javax.swing.JButton saveButton;
    protected javax.swing.JMenuItem saveMenuItem;
    protected javax.swing.JMenuItem sortNameMenuItem;
    protected javax.swing.JMenuItem sortPostcodeMenuItem;
}

如果有人可以提供帮助,那就太好了

4

2 回答 2

2

此行抛出异常:

refreshButton.setText("Refresh");

因为refreshButton没有被实例化。您可以使用:

refreshButton = new JButton();

请参阅:如何阅读和理解 java 堆栈跟踪?

于 2013-01-21T11:25:01.360 回答
0

刷新按钮为空。做这个:

refreshButton = new javax.swing.JButton();

就在之前refreshButton.setText("Refresh");

于 2013-01-21T11:29:01.493 回答