JButton + controller

Résolu
adrienr11vdv Messages postés 99 Date d'inscription jeudi 13 juillet 2006 Statut Membre Dernière intervention 16 avril 2009 - 7 févr. 2008 à 18:48
adrienr11vdv Messages postés 99 Date d'inscription jeudi 13 juillet 2006 Statut Membre Dernière intervention 16 avril 2009 - 7 févr. 2008 à 19:41
Bonjour à tous,
pour une question d'organisation, je désire séparer le code graphique du reste. Pour cela, j'ai crée un package qui gère les JFrame et un autre package qui gère les Controller.

voici ma classe de controlle:
public class LogController implements ActionListener {
    private LogVue logVue;
    private Tout tout;
   
    public LogController(LogVue logVue){
        this.logVue = logVue;
        logVue.getJb_valid().addActionListener(this);
    }

    public void actionPerformed(ActionEvent act) {
        Object src = act.getSource();
    // la source est le bouton valider ::
        if ( src.equals(logVue.getJb_valid()) ){
            tout.setVisible(true);
          }

voici la classe LogVue

public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                LogVue inst = new LogVue();
                inst.setLocationRelativeTo(null);
                inst.setVisible(true);
            }
        });
    }
   
    public LogVue() {
        super();
        initGUI();
        control = new LogController(this);
    }
   
    private void initGUI() {
        try {
            setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
            getContentPane().setLayout(null);
        {
                jb_valid = new JButton();
                getContentPane().add(jb_valid);
                jb_valid.setText("Valider");
                jb_valid.setBounds(85, 121, 79, 29);
            }
   
        public JButton getJb_valid() {
        return jb_valid;
    }
......}

et voici l'erreur:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Controller.LogController.actionPerformed(LogController.java:39)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(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)

merci d'avance pour votre aide

3 réponses

plop3000 Messages postés 135 Date d'inscription mercredi 23 mai 2007 Statut Membre Dernière intervention 7 avril 2012 1
7 févr. 2008 à 18:56
Bonjour,

je crois que tu n'initialise pas ton objet "tout". Donc quand tu fais "tout.setVisible(true)" ça plante. Il faut que tu fasse un new sur "tout" à un moment ou un autre.

J'espère que ça t'aide.

Bonne soirée
3
Twinuts Messages postés 5375 Date d'inscription dimanche 4 mai 2003 Statut Modérateur Dernière intervention 14 juin 2023 111
7 févr. 2008 à 19:35
Salut,

+1 pour l'objet "tout" qui n'est jamais instancié dans le code que tu montres.

------------------------------------
"On n'est pas au resto : ici on ne fait pas dans les plats tout cuits ..."

OoWORAoO
0
adrienr11vdv Messages postés 99 Date d'inscription jeudi 13 juillet 2006 Statut Membre Dernière intervention 16 avril 2009
7 févr. 2008 à 19:41
Merci
0
Rejoignez-nous