Erreur incomprise sur les checkBox

Résolu
Basounours Messages postés 4 Date d'inscription mercredi 3 mai 2006 Statut Membre Dernière intervention 8 mai 2006 - 8 mai 2006 à 15:31
scaryman Messages postés 492 Date d'inscription vendredi 30 janvier 2004 Statut Membre Dernière intervention 16 mai 2007 - 8 mai 2006 à 17:10
Bonjour, je suis un debutant en IHM et j'essaie de faire on application assez simple.



La 1ere interface doit me diriger sur une 2ème selon le resultat d'un Checkbox.



le probleme est que j'ai cette erreur

<hr size="2" width="100%">Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

at IHM.Bouton_Menu.getChoix(Menu_Ajout.java:69)

at IHM.Bouton_Menu.actionPerformed(Menu_Ajout.java:63)

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.pumpOneEventForHierarchy(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%">

Ne connaissant rien au thread ... je pige pas grand chose.



voici mon code.



<hr size="2" width="100%">package IHM;



import java.awt.event.*; // Pour le ActionListener

import java.awt.*; // Pour le container

import javax.swing.*;

import javax.swing.border.Border;



class Bouton_Menu extends JPanel

implements ActionListener // interface écouteur d'événements

{

private CheckboxGroup choix;

private Checkbox ChoixManga;

private Checkbox ChoixPerso;

private Checkbox ChoixAuteur;

private Checkbox ChoixAttaque;

private Checkbox ChoixLieu;

private Checkbox ChoixObjet;



private JButton BoutonOk;

private JButton BoutonKo;

private JLabel test;



public Bouton_Menu()

{

super(new GridLayout(0,1));

// Création des boutons radios

CheckboxGroup choix = new CheckboxGroup();

ChoixManga = new Checkbox("Manga", choix, true);

ChoixPerso = new Checkbox("Personnage", choix, false);

ChoixAuteur = new Checkbox("Auteur", choix, false);

ChoixAttaque = new Checkbox("Attaque", choix, false);

ChoixLieu = new Checkbox("Lieu", choix, false);

ChoixObjet = new Checkbox("Objet", choix, false);

// Création des boutons OK et KO

BoutonOk = new JButton("Ok");

BoutonKo = new JButton("Quitter");

test = new JLabel("aaaa");



// Insertion des boutons dans l'objet Panel_Menu

add(ChoixManga);

add(ChoixPerso);

add(ChoixAuteur);

add(ChoixAttaque);

add(ChoixLieu);

add(ChoixObjet);

add(BoutonOk);

add(BoutonKo);

add(test);

// Les sources d'événements sont déclarées à l'écouteur

BoutonOk.addActionListener(this);

BoutonKo.addActionListener(this);

}



public void actionPerformed(ActionEvent evt)

// Permet de traiter l'événement en fonction de l'objet source

{

if (evt.getSource() == BoutonKo)

{

System.exit(0);

}

if (evt.getSource() == BoutonOk)

{

test.setName(getChoix());

}

}



public String getChoix()

{

return choix.getSelectedCheckbox().getLabel().toString();

}

}



class Panel_Menu extends JFrame

{

public Panel_Menu()

{

setTitle("Menu Ajout");

this.setLocation(150, 150);

setSize(300, 250);

Container c = getContentPane();

c.add(new JLabel("Séléctionner l'objet que vous voulez Ajouter"),BorderLayout.NORTH);

c.add(new Bouton_Menu(),BorderLayout.WEST);

setVisible(true);

}

}



public class Menu_Ajout extends JFrame

{

public static void main(String[]telsArgs)

{

//Appel de la classe Panel_Menu

new Panel_Menu();

}//main

}//classe Start



<hr size="2" width="100%">



Voila merci ^^

3 réponses

scaryman Messages postés 492 Date d'inscription vendredi 30 janvier 2004 Statut Membre Dernière intervention 16 mai 2007 12
8 mai 2006 à 17:10
Salut
Il y a un problème : tu déclares 2 fois un CheckboxGroup choix
Il faut corriger dans le constructeur :
choix = new CheckboxGroup();

Voila
A++
3
yannick_parchemal Messages postés 31 Date d'inscription jeudi 13 avril 2006 Statut Membre Dernière intervention 22 mai 2006
8 mai 2006 à 16:18
L'erreur est à la ligne :
return choix.getSelectedCheckbox().getLabel().toString();

tu a oublier d'ajouter tes checkbox à la ChecbokGroup
donc choix.getSelectedCheckbox() rend null
et choix.getSelectedCheckbox().getLabel() provoque l'erreur
0
Basounours Messages postés 4 Date d'inscription mercredi 3 mai 2006 Statut Membre Dernière intervention 8 mai 2006
8 mai 2006 à 16:41
c'est pas comme ca qu'on fait ???



ChoixManga = new Checkbox("Manga", choix, true);

ChoixPerso = new Checkbox("Personnage", choix, false);

ChoixAuteur = new Checkbox("Auteur", choix, false);

ChoixAttaque = new Checkbox("Attaque", choix, false);

ChoixLieu = new Checkbox("Lieu", choix, false);



je les ajoute pourtant a choix qui est mon groupe ?

je connais pas d'autre solution pour le faire.

peut tu m'eclairer ???
0
Rejoignez-nous