0

I am trying to write a program for a simple calculator that just adds two numbers and shows it in a JLabel. I have managed to design a working Window, Button, etc. But if I click the Button the Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException - Error happens and I don´t know how to solve it.

Here is the code :

import java.awt.EventQueue;

public class Rechnerwin {

private JFrame frame;
private JTextField textFielda;
private JTextField textFieldb;
private JLabel label;
int A;
int B;
int C;
String a;
String b;
String c;


public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Rechnerwin window = new Rechnerwin();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }); 
}

public Rechnerwin() {

    initialize();
}


private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);

    textFielda = new JTextField();
    textFielda.setBounds(44, 41, 86, 20);
    frame.getContentPane().add(textFielda);
    textFielda.setColumns(10);

    textFieldb = new JTextField();
    textFieldb.setBounds(307, 41, 86, 20);
    frame.getContentPane().add(textFieldb);
    textFieldb.setColumns(10);

    JButton btnAdd = new JButton("Add");
    btnAdd.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            String a = textFielda.getText();
            String b = textFieldb.getText();
            int A = Integer.parseInt(a);
            int B = Integer.parseInt(b);
            int C = A + B;
            String c = Integer.toString(C);
            label.setText(c);
        }
    });
    btnAdd.setBounds(169, 85, 89, 23);
    frame.getContentPane().add(btnAdd);

    JLabel label = new JLabel("a");
    label.setBounds(146, 184, 131, 20);
    frame.getContentPane().add(label);
  }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
DerMaksi
  • 3
  • 2
  • 1
    If you acknowledge that is was asked before and you know is has been? Why did you still ask it? Just read the previous answer – Andrew Li Jun 13 '16 at 19:42
  • 1
    http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Michael Jun 13 '16 at 19:45
  • As i mentioned in my text I looked for solutions ,but coudn´t find anything ,that could help me. – DerMaksi Jun 13 '16 at 19:47
  • Please run a debugger and indicate where the failure occurs and perhaps inspect your objects thoroughly while doing so, we need this information in order to provide a useful answer. A stack trace would be nice, too. – Brandon McKenzie Jun 13 '16 at 19:51
  • Try to take a look at the question linked by @Michael and follow the instructions to obtain the stacktrace of your exception. This will be needed to provide further assistance... – dpr Jun 13 '16 at 19:53
  • I don't think this is a dupe as the linked question is more like a howto on obtaining the stacktrace information, but is of little help only for fixing a concrete `NullPointerException` as in this case. – dpr Jun 13 '16 at 20:04
  • This is clearly a duplicate of the referenced question. The `label` variable is used at the end of the button's `actionPerformed`, but the global label was never initialized; only the locally-defined label was, a few lines later. This is where the NullPointerException is coming from. To confirm, comment out that line and re-run. The error will be gone, but you won't have text in the label. After you've confirmed this, you can uncomment the line again and fix the redundant variable names for `label`. – Michael Gaskill Jun 13 '16 at 21:43

2 Answers2

0

try moving the label before the listener in the Button

Example:

JLabel label = new JLabel("a");
label.setBounds(146, 184, 131, 20);
frame.getContentPane().add(label);

JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

    String a = textFielda.getText();
    String b = textFieldb.getText();
    int A = Integer.parseInt(a);
    int B = Integer.parseInt(b);
    int C = A + B;
    String c = Integer.toString(C);
    label.setText(c);
    }
    });
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

The problem probably is that in the listener you try to set the text for the member label of your Rechnerwin class. But this member is never initialized. After attaching the button listener you are adding a new JLabel to your UI with the same name as your member label. But these are two different variables and only the local one gets initialized.

You will probably solve this by this change:

...
label = new JLabel("a");
label.setBounds(146, 184, 131, 20);
...
dpr
  • 10,591
  • 3
  • 41
  • 71