2011-07-29 2 views
0

S'il vous plaît aidez-moi à comprendre ce qui ne va pas ici? juste débutant en Java.Composants non initialisés lancer 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

Il n'y a pas 'NullPointerException' quand je lance ce code, pourriez-vous ajouter stacktrace et plus d'explications ce qui est mauvais à votre poste. ? – Sorceror

+0

Ajouter plus de tags dans le futur. –

+0

Ca marche pour moi! Je l'ai démarré depuis Eclipse et en ligne de commande:

Répondre

0

Pas de problème dans ce code. n'importe quel moyen d'exécuter ce code. signifie utiliser cmd.

cmd commande combile:

javac PropertyChangeListenerTest.java

java PropertyChangeListenerTest

Questions connexes