2011-07-29 102 views
0

請幫我弄清楚這裏出了什麼問題?剛開始用java。組件未初始化投擲NUllPointerException

public class PropertyChangeListenerTest { 
private JPanel mainPanel = null; 
private JTextField source = null; 
private JTextField target = null; 

PropertyChangeListenerTest() { 

    mainPanel = new JPanel(); 
    mainPanel.setLayout(new GridLayout(1, 0, 10, 0)); 
    source = new JTextField(20); 
    target = new JTextField(20); 
    mainPanel.add(source); 
    mainPanel.add(target); 

} 

JPanel getMainPanel() { 
    return mainPanel; 
} 

public static void main(String[] args) { 
    SwingUtilities.invokeLater(new Runnable() { 

     @Override 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

protected static void createAndShowGUI() { 
    JFrame f = new JFrame("PropertyChangeListener"); 
    f.getContentPane().add(new PropertyChangeListenerTest().getMainPanel()); 
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    f.pack(); 
    f.setVisible(true); 
} 

}

+0

有沒有'NullPointerException'當我運行此代碼,您可以加入堆棧跟蹤和更多的解釋什麼是壞到您的文章。 。? – Sorceror

+0

將來添加更多標籤。 –

+0

它適合我!我從Eclipse和命令行啓動它:

回答

0

在這段代碼沒有問題。任何方式來運行此代碼。意味着使用cmd。

CMD命令combile:

的javac PropertyChangeListenerTest.java

的Java PropertyChangeListenerTest