-3

ok so I'm coding my own version of a GUI based addition calculator. and I've come upon an error . but when I run it , it gives a null pointer exception at the line where I marked in the code and i know null pointer means there is no value for a certain string or integer but this one here does have a value , if someone knows why pls tell me , thanks in advance!

`public class Main implements ActionListener {
private static JFrame frame;
private static JPanel panel;
private static JLabel label;
private static JButton button;
private static JTextField fnumField;
private static JTextField snumField;
private static JLabel answer;
private static  JLabel fnum;
private static JLabel snum;
private static String c;

public static void main(String[] args) {
    
    
    frame = new JFrame();
    panel = new JPanel();
    frame.setSize(350, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(panel);
    
    panel.setLayout(null);

    
    JLabel fnum  = new JLabel("1st number:");
    fnum.setBounds(10, 20, 80, 25);
    panel.add(fnum);
    
    JTextField fnumField = new JTextField();
    fnumField.setBounds(100, 20, 165, 25);
    panel.add(fnumField);
    
    JLabel snum = new JLabel("2nd number:");
    snum.setBounds(10, 50, 80, 25);
    panel.add(snum);
    
    JTextField snumField = new JTextField();
    snumField.setBounds(100, 50, 165, 25);
    panel.add(snumField);
    
    button = new JButton("ADD");
    button.setBounds(10, 80, 80, 25);
    button.addActionListener(new Main ());
    panel.add(button);
    
    JLabel answer = new JLabel("");
    answer.setBounds(10, 100, 300, 25);
    panel.add(answer);          
        
        
        answer.setText(c);
        frame.setVisible(true);

    }
    


public void actionPerformed(ActionEvent e) {
    String a = fnumField.getText();             <---------- NULL POINTER 
    String b = snumField.getText();
    
    String c = (a+b);
    
}   

}

`

1 Answers1

0
JLabel fnum  = new JLabel("1st number:");
JTextField fnumField = new JTextField();
JLabel snum = new JLabel("2nd number:");
JTextField snumField = new JTextField();
JLabel answer = new JLabel("");

At all of these, remove the type, otherwise you declare a local variable, write to it, and the field is not set. Change it to:

fnum  = new JLabel("1st number:");
fnumField = new JTextField();
snum = new JLabel("2nd number:");
snumField = new JTextField();
answer = new JLabel("");
JCWasmx86
  • 3,473
  • 2
  • 11
  • 29
  • ok i did that but now when i run it and give values for fnum and snum and click the button nothing even happens......help? – Niteesh Kumar Nov 07 '20 at 10:21
  • That's because you don't change any field in your action listener. – JCWasmx86 Nov 07 '20 at 10:21
  • And `String c = (a+b);` is not doing what you think. If a is 19 and b is 20, the result would be 1920. You are looking for `String c=(Integer.parseInt(a)+Integer.parseInt(b))+""`.(+ Exception handling) – JCWasmx86 Nov 07 '20 at 10:22
  • You have to use for example `answer.setText(c)` in your action listener – JCWasmx86 Nov 07 '20 at 10:23
  • bro thanks a lot man I'm new to java just started 3 days ago and wanted to try this GUI calculator thing and i was stuck at this bug and u cleared it for me , thanks a lot my g – Niteesh Kumar Nov 07 '20 at 10:29
  • If I could help you consider [accepting](https://stackoverflow.com/help/accepted-answer) it. – JCWasmx86 Nov 07 '20 at 10:30