0

I have tried to go over this my self but i do not know how to resolve this.

I am getting this error when i try and run my addressBookUi class.

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)

Here is my addressBookUi class

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

If anyone could help that would be great

Peaches
  • 5
  • 1
  • 4
  • BTW, do you know how to read Exceptions? It will be very useful to learn. In the exception you get it is stated `at addressBookUI.startComponents(addressBookUI.java:43)`, which is a clear indication of the exact line in which the problem occurs (I cannot be sure, but I bet line 43 is `refreshButton.setText("Refresh");`. This is how Reimeus could reply to your question. – ThanksForAllTheFish Jan 21 '13 at 11:29

2 Answers2

2

This line is throwing the exception:

refreshButton.setText("Refresh");

as refreshButton has not been instantiated. You can use:

refreshButton = new JButton();

See: How to read and understand the java stack trace?

Community
  • 1
  • 1
Reimeus
  • 158,255
  • 15
  • 216
  • 276
0

refreshButton is null. Do this:

refreshButton = new javax.swing.JButton();

just before refreshButton.setText("Refresh");

S.Yavari
  • 876
  • 8
  • 25