简体   繁体   中英

Property change event handler vs button click in eclipse window builder?

I recently created a method that adds two doubles that were inputted via two formatted text fields, and outputs the result in an output text area.

Here is the code I have so far:

protected void do_btnAdd_actionPerformed(ActionEvent e) {
    num1 = ((Number)(firstFT.getValue())).doubleValue();
    num2 = ((Number)(secondFT.getValue())).doubleValue();
    double result = num1 + num2;
    String answer = Double.toString(result);
    output.setText(answer);
}

I get an error on the first line of the method when I use the property change event handler.

My question is how could I get this to work by using another event handler such as property change?

It runs fine with a button I just wanted to try it a different way, hoping that someone else had a similar problem.

Thanks!

You have to check whether your text fields are empty or not. If you try to convert a null to double it will throws an NullPointerException .

Try your property change event like this:

if(!firstFT.getText().equals("") && !secondFT.getText().equals("")){
    double num1 = ((Number)(firstFT.getValue())).doubleValue();
    double num2 = ((Number)(secondFT.getValue())).doubleValue();
    double result = num1 + num2;
    String answer = Double.toString(result);
    output.setText(answer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM