2013-01-21 71 views
0

我試圖過這個我的自我,但我不知道如何解決這個問題。線程「AWT-EventQueue-0」中的Java異常java.lang.NullPointerException錯誤

我遇到這個錯誤,當我嘗試並運行我的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) 

這裏是我addressBookUi類

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

如果有人能夠幫助這將是偉大

+0

之前順便說一句,你知道如何閱讀例外?學習會非常有用。在例外情況下,您可以在addressBookUI.startComponents(addressBookUI.java:43)處聲明'',這清楚地表明瞭問題發生的確切路線(我無法確定,但我敢肯定,第43行是'refreshButton .setText(「Refresh」);'。這就是Reimeus如何回答你的問題。 – ThanksForAllTheFish

回答

0

refreshButton爲空。這樣做:

refreshButton = new javax.swing.JButton(); 

只是refreshButton.setText("Refresh");

相關問題