-1

I have to create a calculator that displays the results according to their text fields. The display does not work, yet the calculation is performed. I get an error that says my JLabel is zero. How do I solve the problem?

Maybe with

JLabel Resultat = new JLabel("Résultat : ", SwingConstants.CENTER);

at the wrong place ?

The code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Exercice4 extends JFrame {

 private JTextField ZoneTxt, ZoneTxt2;
 private JPanel CalculPanel;
 private JLabel Resultat;
 private JComboBox Op;
 private JButton BCalcul;
 private int cbselect = 0;
 private double res;

 final String[] operateur = { "+", "-", "*", "/" };

 public Exercice4() {

  super("Exercice 4");
  JFrame FrameExercice4 = new JFrame();
  JPanel CalculPanel = new JPanel();
  ZoneTxt = new JTextField(10);
  ZoneTxt2 = new JTextField(10);
  final JComboBox Op = new JComboBox(operateur);

  BCalcul = new JButton("Calculer");

  BCalcul.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {

    int cbselect = Op.getSelectedIndex();

    if (cbselect == 0) {

     res = (((Double.parseDouble(ZoneTxt.getText()))) + ((Double
       .parseDouble(ZoneTxt2.getText()))));

     System.out.println(res);
     Resultat.setText("Résultat : " + String.valueOf(res));
    }
    if (cbselect == 1) {
     // System.out.println(res1 - res2);

    }
    if (cbselect == 2) {
     // System.out.println(res1 * res2);

    }
    if (cbselect == 3) {
     // System.out.println(res1 / res2);

    }

   }

  });

  JLabel Resultat = new JLabel("Résultat : ", SwingConstants.CENTER);

  FrameExercice4.setVisible(true);
  FrameExercice4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  FrameExercice4.setSize(500, 500);

  FrameExercice4.add(CalculPanel);
  CalculPanel.add(ZoneTxt);
  CalculPanel.add(Op);
  CalculPanel.add(ZoneTxt2);
  CalculPanel.add(BCalcul);
  CalculPanel.add(Resultat, BorderLayout.CENTER);

 }

 public static void main(String[] args) {
  JFrame frame = new Exercice4();

 }

}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
  • `I get an error that says my JLabel is zero` Seems there would be a NullPointerException thrown in there as well – copeg May 01 '15 at 17:01
  • Yes, thats the error – user3592221 May 01 '15 at 17:04
  • Please edit your question (by pressing the "edit" link under the rectangular tags at the bottom of your question) and include the *entire stack trace* of the NullPointerException. That exception is telling you exactly what went wrong, and where. It will dramatically reduce the time needed to solve the problem. – VGR May 01 '15 at 17:09

2 Answers2

0

It works. I had to do that:
final JLabel Resultat

  • See other answer. This may fix, but if you try to refer to your Resultat field elsewhere you may encounter the same issue. – copeg May 01 '15 at 17:11
0

Instead of creating new variables in your constructor, instantiate the field:

private JLabel Resultat;//field

....
//below creates a new variable
//JLabel Resultat = new JLabel("Résultat : ", SwingConstants.CENTER);
//below instantiates the field
Resultat = new JLabel("Résultat : ", SwingConstants.CENTER);

See What is a NullPointerException, and how do I fix it?

Community
  • 1
  • 1
copeg
  • 8,290
  • 19
  • 28