Appelle d'1 object dans une frame java
Résolu
siba
Messages postés
35
Date d'inscription
Statut
Membre
Dernière intervention
-
siba Messages postés 35 Date d'inscription Statut Membre Dernière intervention -
siba Messages postés 35 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
j'ai 1 probleme ,j'ai une classe qui marche compilé et executer et quand je fait appele à un instance de cette classe dans une frame qui contient des jtext field 1 bouton et des choices et pricisement dans la methode action performed qui fait l'action sur un bouton pour obtenir le resultat
de la methode
public class FrameBasculeD{
................
..............
................
public void actionPerformed (ActionEvent ev){
BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()),(byte) (choice2.getSelectedIndex()));
if(ev.getSource()==bouton){
BD.calculerQ2();
String s1=String.valueOf(BD.getQ2());
text5.setText(s1);
}
}
la compilation :Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
//la class BasculeD_CLK n'est pas null et ses methode marche bien et j'ai les testé
//s'il vous plait proposez moi des solution
//merci d'avance
j'ai 1 probleme ,j'ai une classe qui marche compilé et executer et quand je fait appele à un instance de cette classe dans une frame qui contient des jtext field 1 bouton et des choices et pricisement dans la methode action performed qui fait l'action sur un bouton pour obtenir le resultat
de la methode
public class FrameBasculeD{
................
..............
................
public void actionPerformed (ActionEvent ev){
BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()),(byte) (choice2.getSelectedIndex()));
if(ev.getSource()==bouton){
BD.calculerQ2();
String s1=String.valueOf(BD.getQ2());
text5.setText(s1);
}
}
la compilation :Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
//la class BasculeD_CLK n'est pas null et ses methode marche bien et j'ai les testé
//s'il vous plait proposez moi des solution
//merci d'avance
A voir également:
- Appelle d'1 object dans une frame java
- Waptrick java football - Télécharger - Jeux vidéo
- Jeux java itel - Télécharger - Jeux vidéo
- Nommez une application d'appel vidéo ou de visioconférence - Guide
- Eclipse java - Télécharger - Langages
- Java apk - Télécharger - Langages
3 réponses
choice1, choice2 et choice3 ont-il toujour une valeur?
le null exception peut venir des choices, regarde comme même.
String.valueOf(BD.getQ2());
""+BD.getQ2(); // marche tout aussi bien ^^
le null exception peut venir des choices, regarde comme même.
String.valueOf(BD.getQ2());
""+BD.getQ2(); // marche tout aussi bien ^^
oui ,oui et la methode getQ2() marche trés bien ,je pense que le prob c dans le constructeur car quand je l'ai mis dans le contructeur, la methode marche et elle me donne le resultat dans le JText field mais pour le premier clic sur le bouton le dexieme clic ne donne rien c comme si les valeurs des troix choices sont fixé dans les"0",apres jé changé et je l'ai mis dans la methode action performed mais pas de resultat
voici mon ncode :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameBasculeD extends JFrame implements ActionListener{
private Image image;
private PaneauD paneau;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private Choice choice1;
private Choice choice2;
private Choice choice3;
private JButton bouton;
private Font f;
private BasculeD_CLK BD;
public FrameBasculeD(){
setBounds(100,100,700,600);
setTitle("Bascule D");
f=new Font("Serif",Font.BOLD,18);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
text1=new JTextField(" D ");
text1.setBounds(40,160,50,30);
text1.setForeground(Color.white);
text1.setBackground(Color.black);
text1.setFont(f);
text1.setEnabled(false);
add (text1);
text2=new JTextField(" CLK ");
text2.setBounds(40,250,50,30);
text2.setForeground(Color.white);
text2.setBackground(Color.black);
text2.setFont(f);
text2.setEnabled(false);
add (text2);
text3=new JTextField(" Q+ ");
text3.setBounds(570,170,50,30);
text3.setForeground(Color.white);
text3.setBackground(Color.black);
text3.setFont(f);
text3.setEnabled(false);
add (text3);
text4=new JTextField(" Q- ");
text4.setBounds(570,310,50,30);
text4.setForeground(Color.green);
text4.setBackground(Color.black);
text4.setFont(f);
text4.setEnabled(false);
add (text4);
text5=new JTextField();
text5.setBounds(500,310,60,30);
text5.setForeground(Color.red);
text5.setBackground(Color.black);
text5.setFont(f);
// text5.setEnabled(false);
add (text5);
Choice choice1=new Choice();
choice1.setBounds(100,160,50,30);
choice1.setBackground(Color.black);
choice1.setForeground(Color.green);
choice1.setFont(f);
choice1.addItem("0");
choice1.addItem("1");
add(choice1);
Choice choice2=new Choice();
choice2.setBounds(100,250,50,30);
choice2.setBackground(Color.black);
choice2.setForeground(Color.green);
choice2.setFont(f);
choice2.addItem("0");
choice2.addItem("1");
add(choice2);
Choice choice3=new Choice();
choice3.setBounds(520,180,50,30);
choice3.setBackground(Color.black);
choice3.setForeground(Color.green);
choice3.setFont(f);
choice3.addItem("0");
choice3.addItem("1");
add(choice3);
bouton=new JButton("executer");
bouton.setBounds(300,400,100,40);
bouton.addActionListener(this);
bouton.setFont(f);
add(bouton);
image=Toolkit.getDefaultToolkit().getImage("basculeD.jpg");
paneau=new PaneauD(image);
paneau.setBounds(30,30,800,500);
add(paneau);
paneau.repaint();
// BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()),(byte) (choice2.getSelectedIndex()));
}
public void actionPerformed (ActionEvent ev){
BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()), (byte)(choice2.getSelectedIndex()));
if(ev.getSource()==bouton){
BD.calculerQ2();
String s1=String.valueOf(BD.getQ2());
text5.setText(s1);
}
}
}
//s'il vous plait proposez moi une solution
voici mon ncode :
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameBasculeD extends JFrame implements ActionListener{
private Image image;
private PaneauD paneau;
private JTextField text1;
private JTextField text2;
private JTextField text3;
private JTextField text4;
private JTextField text5;
private Choice choice1;
private Choice choice2;
private Choice choice3;
private JButton bouton;
private Font f;
private BasculeD_CLK BD;
public FrameBasculeD(){
setBounds(100,100,700,600);
setTitle("Bascule D");
f=new Font("Serif",Font.BOLD,18);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(null);
text1=new JTextField(" D ");
text1.setBounds(40,160,50,30);
text1.setForeground(Color.white);
text1.setBackground(Color.black);
text1.setFont(f);
text1.setEnabled(false);
add (text1);
text2=new JTextField(" CLK ");
text2.setBounds(40,250,50,30);
text2.setForeground(Color.white);
text2.setBackground(Color.black);
text2.setFont(f);
text2.setEnabled(false);
add (text2);
text3=new JTextField(" Q+ ");
text3.setBounds(570,170,50,30);
text3.setForeground(Color.white);
text3.setBackground(Color.black);
text3.setFont(f);
text3.setEnabled(false);
add (text3);
text4=new JTextField(" Q- ");
text4.setBounds(570,310,50,30);
text4.setForeground(Color.green);
text4.setBackground(Color.black);
text4.setFont(f);
text4.setEnabled(false);
add (text4);
text5=new JTextField();
text5.setBounds(500,310,60,30);
text5.setForeground(Color.red);
text5.setBackground(Color.black);
text5.setFont(f);
// text5.setEnabled(false);
add (text5);
Choice choice1=new Choice();
choice1.setBounds(100,160,50,30);
choice1.setBackground(Color.black);
choice1.setForeground(Color.green);
choice1.setFont(f);
choice1.addItem("0");
choice1.addItem("1");
add(choice1);
Choice choice2=new Choice();
choice2.setBounds(100,250,50,30);
choice2.setBackground(Color.black);
choice2.setForeground(Color.green);
choice2.setFont(f);
choice2.addItem("0");
choice2.addItem("1");
add(choice2);
Choice choice3=new Choice();
choice3.setBounds(520,180,50,30);
choice3.setBackground(Color.black);
choice3.setForeground(Color.green);
choice3.setFont(f);
choice3.addItem("0");
choice3.addItem("1");
add(choice3);
bouton=new JButton("executer");
bouton.setBounds(300,400,100,40);
bouton.addActionListener(this);
bouton.setFont(f);
add(bouton);
image=Toolkit.getDefaultToolkit().getImage("basculeD.jpg");
paneau=new PaneauD(image);
paneau.setBounds(30,30,800,500);
add(paneau);
paneau.repaint();
// BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()),(byte) (choice2.getSelectedIndex()));
}
public void actionPerformed (ActionEvent ev){
BD=new BasculeD_CLK((byte)(choice1.getSelectedIndex()),(byte)(choice3.getSelectedIndex()), (byte)(choice2.getSelectedIndex()));
if(ev.getSource()==bouton){
BD.calculerQ2();
String s1=String.valueOf(BD.getQ2());
text5.setText(s1);
}
}
}
//s'il vous plait proposez moi une solution