[Graphic Débutant] Null Pointer Exception

Résolu
emechub Messages postés 7 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 7 mai 2009 - 7 mai 2009 à 19:08
emechub Messages postés 7 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 7 mai 2009 - 7 mai 2009 à 20:04
Bonjour après longuement essayé de trouver d'où venait le Null Pointer
Exception, j'abandonne et je compte sur vous pour me donner un petit
coup de pouce qui, je suppose ne sera pas bien difficile a donner.


J'ai surement un code super moche donc n'hésitez pas a corriger des choses illogiques et me faire comprendre mes erreurs.


PS : L'exo est un convertisseur Livre Sterling -> Euro

<hr size= "2" width="100%" />
1. Ma Classe "ButtonListener"
<ol><li>package convert;</li><li>import java.awt.event.*;</li><li>public class ButtonListener implements ActionListener{</li><li>    protected V1 choix;</li><li>    public ButtonListener(V1 choix){</li><li>        this.choix = choix;</li><li>    }</li><li>    public void actionPerformed(ActionEvent e){</li><li>        if (e.getActionCommand().equals("Euro->Livre")){</li><li>            choix.changeLivres();</li><li>        }else{</li><li>            choix.changeEuros();</li><li>        }</li><li>    }</li><li>}</li></ol>
<hr size ="2" width= "100%" />2. Ma Classe principale "V1"
<ol><li>package convert;</li><li>import java.awt.*;</li><li>import java.awt.event.*;</li><li>import javax.swing.*;</li><li>public class V1 extends JPanel{</li><li>    protected JLabel euros, livres;</li><li>    protected JTextField eurosField,livresField;</li><li>    protected JButton ver1,ver2;</li><li>    public V1(){</li><li>        setLayout(new GridLayout(3,2));</li><li>        </li><li>        /** CREATION **/</li><li>        JLabel euros new JLabel("Euros");</li><li>        JLabel livres new JLabel("Livres");</li><li>        </li><li>        JTextField eurosField = new JTextField("0");</li><li>        JTextField livresField = new JTextField("0");</li><li>        System.out.println(""+livresField.getText());</li><li>        JPanel bouttonLE = new JPanel();</li><li>        JPanel bouttonEL = new JPanel();</li><li>        JButton ver1 = new JButton("Euro->Livre");</li><li>        JButton ver2 = new JButton("Livre->Euro");</li><li>        ActionListener listener = new ButtonListener(this);</li><li>        ver1.addActionListener(listener);</li><li>        ver2.addActionListener(listener);</li><li>        bouttonLE.add(ver1);</li><li>        bouttonLE.add(ver2);</li><li>        bouttonEL.add(ver2);</li><li>        /** PLACEMENT **/</li><li>        add(eurosField);</li><li>        add(euros);</li><li>        add(ver1);</li><li>        add(ver2);</li><li>        add(livresField);</li><li>        add(livres);</li><li>    }</li><li>    public void changeEuros(){</li><li>        double newValue = Double.parseDouble(livresField.getText());</li><li>        eurosField.setText(""+newValue);</li><li>    }</li><li>    public void changeLivres(){</li><li>        double newValue = Double.parseDouble(eurosField.getText());</li><li>        livresField.setText(""+newValue);</li><li>    }</li><li>    public static void main(String args[]){</li><li>        JFrame f = new JFrame("BenOOp's Convert");</li><li>        f.getContentPane().add(new V1());</li><li>        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</li><li>        f.pack();</li><li>        f.setVisible(true);</li><li>    }</li><li>}</li></ol> <hr size ="2" width="100%" /> Merci pour votre aide

2 réponses

emechub Messages postés 7 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 7 mai 2009
7 mai 2009 à 20:04
J'ai vraiment honte de mon erreur mais je l'ai corrigée il suffisait d'initialiser mes variables

    protected JTextField eurosField = new JTextField("0");
    protected JTextField livresField = new JTextField("0");

en global et non pas en local.
Enfin si j'ai bien compris....

Bref merci quand même, soyez pas frustrés j'ai été plus rapide que vous sur ce coup :p
Je vous accorderais sûrement une revanche un jour :p
3
emechub Messages postés 7 Date d'inscription mercredi 13 février 2008 Statut Membre Dernière intervention 7 mai 2009
7 mai 2009 à 19:17
Je ne trouve pas où éditer mon message donc je vous le dit ici :

Mon code compile, et s'exécute normalement jusqu'à ce que je clique sur "Livre->Euro" ou "Euro->Livre", là le programme me met un gros paquet d'erreur :

<hr size="2" width="100%" />Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at convert.V1.changeLivres(V1.java:48)
    at convert.ButtonListener.actionPerformed(ButtonListener.java:12)
    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)
<hr size="2" width="100%" />
0
Rejoignez-nous