如何摆脱动作事件中的空指针异常?

时间:2017-05-04 06:17:57

标签: java user-interface javafx nullpointerexception fxml

我试图点击一下按钮,将用户输入文本框的内容添加到另一个文本框中。我使用FXML,我已将on动作设置为calculateTotal()。当我在运行时单击按钮时,我会在行

上获得空指针激活
 private static Query parseQuery(string searchQuery, QueryParser parser)
 {
    Query query;
    try
    {
      query = parser.Parse(searchQuery);
    }
    catch (ParseException e)
    {
      query = null;
    }
    if (query == null)
    {
      string replacedQuery;
      cooked = Regex.Replace(searchQuery, @"\B[A-Z]", m => "" + m.ToString().ToLower());
      cooked = Regex.Replace(cooked, "[^a-zA-Z0-9]+", " ");
      query = parser.Parse(replacedQuery);
    }
    return query;
}

这是代码的其余部分。错误所在的行是calculateTotal()中的底部。

int sal1 = Integer.parseInt(sale1.getText());

这是我的fxml文件。按钮位于组合框之后

package taxerfxml;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javax.swing.JComboBox;
import static sun.misc.Signal.handle;


/**
 *
 * @author 
 */
public class FXMLDocumentController implements Initializable  {
    ObservableList<String> comboBoxList = FXCollections.observableArrayList("Ccosta", "Alam", "Sclara", "Smateo", "Sf", "Marin", "Sonoma", "Napa", "Solono");

//fxml combo boxes
    @FXML
    private ComboBox comboBox1;
    @FXML
    private ComboBox comboBox2;
    @FXML
    private ComboBox comboBox3;
    @FXML
    private ComboBox comboBox4;
    @FXML
    private ComboBox comboBox5;
    @FXML
    private ComboBox comboBox6;
    @FXML
    private ComboBox comboBox7;
    @FXML
    private ComboBox comboBox8;
    @FXML
    private ComboBox comboBox9;
    @FXML
    private ComboBox comboBox10;
    @FXML
    private ComboBox comboBox11;
    @FXML
    private ComboBox comboBox12;

//fxml labels
    @FXML
    private Label sale;    
    @FXML
    private Label tax;
    @FXML
    private Label county;  
    @FXML
    private Label freight;
    @FXML
    private Label labor;
    @FXML
    private Label cost;
    @FXML
    private Label profit;
    @FXML
    private Label customer;

    // fxml customer textfields
    @FXML
    private TextField customer1;
    @FXML
    private TextField customer2;
    @FXML
    private TextField customer3;
    @FXML
    private TextField customer4;
    @FXML
    private TextField customer5;
    @FXML
    private TextField customer6;
    @FXML
    private TextField customer7;
    @FXML
    private TextField customer8;
    @FXML
    private TextField customer9;
    @FXML
    private TextField customer10;
    @FXML
    private TextField customer11;
    @FXML
    private TextField customer12;

    //fxml sale textfields

    @FXML
    private TextField sale1;
    @FXML
    private TextField sale2;
    @FXML
    private TextField sale3;
    @FXML
    private TextField sale4;
    @FXML
    private TextField sale5;
    @FXML
    private TextField sale6;
    @FXML
    private TextField sale7;
    @FXML
    private TextField sale8;
    @FXML
    private TextField sale9;
    @FXML
    private TextField sale10;
    @FXML
    private TextField sale11;
    @FXML
    private TextField sale12;

    //fxml tax textfields 

    @FXML
    private TextField tax1;
    @FXML
    private TextField tax2;
    @FXML
    private TextField tax3;
    @FXML
    private TextField tax4;
    @FXML
    private TextField tax5;
    @FXML
    private TextField tax6;
    @FXML
    private TextField tax7;
    @FXML
    private TextField tax8;
    @FXML
    private TextField tax9;
    @FXML
    private TextField tax10;
    @FXML
    private TextField tax11;
    @FXML
    private TextField tax12;

    //fxml freight textfields
    @FXML
    private TextField freight1;
    @FXML
    private TextField freight2;
    @FXML
    private TextField freight3;
    @FXML
    private TextField freight4;
    @FXML
    private TextField freight5;
    @FXML
    private TextField freight6;
    @FXML
    private TextField freight7;
    @FXML
    private TextField freight8;
    @FXML
    private TextField freight9;
    @FXML
    private TextField freight10;
    @FXML
    private TextField freight11;
    @FXML
    private TextField freight12;

    //fxml labor textfields
    @FXML
    private TextField labor1;
    @FXML
    private TextField labor2;
    @FXML
    private TextField labor3;
    @FXML
    private TextField labor4;
    @FXML
    private TextField labor5;
    @FXML
    private TextField labor6;
    @FXML
    private TextField labor7;
    @FXML
    private TextField labor8;
    @FXML
    private TextField labor9;
    @FXML
    private TextField labor10;
    @FXML
    private TextField labor11;
    @FXML
    private TextField labor12;

    //fxml cost textfields
    @FXML
    private TextField cost1;
    @FXML
    private TextField cost2;
    @FXML
    private TextField cost3;
    @FXML
    private TextField cost4;
    @FXML
    private TextField cost5;
    @FXML
    private TextField cost6;
    @FXML
    private TextField cost7;
    @FXML
    private TextField cost8;
    @FXML
    private TextField cost9;
    @FXML
    private TextField cost10;
    @FXML
    private TextField cost11;
    @FXML
    private TextField cost12;

    //fxml profit textfields
    @FXML
    private TextField profit1;
    @FXML
    private TextField profit2;
    @FXML
    private TextField profit3;
    @FXML
    private TextField profit4;
    @FXML
    private TextField profit5;
    @FXML
    private TextField profit6;
    @FXML
    private TextField profit7;
    @FXML
    private TextField profit8;
    @FXML
    private TextField profit9;
    @FXML
    private TextField profit10;
    @FXML
    private TextField profit11;
    @FXML
    private TextField profit12;

    //fxml button calculate   
   @FXML
    private Button calculate;




    @Override
    public void initialize(URL url, ResourceBundle rb) {



         //initialize combobox variables
        comboBox1.setValue("County");
        comboBox1.setItems(comboBoxList);

        comboBox2.setValue("County");
        comboBox2.setItems(comboBoxList);

        comboBox3.setValue("County");
        comboBox3.setItems(comboBoxList);

        comboBox4.setValue("County");
        comboBox4.setItems(comboBoxList);

        comboBox5.setValue("County");
        comboBox5.setItems(comboBoxList);

        comboBox6.setValue("County");
        comboBox6.setItems(comboBoxList);

        comboBox7.setValue("County");
        comboBox7.setItems(comboBoxList);

        comboBox8.setValue("County");
        comboBox8.setItems(comboBoxList);

        comboBox9.setValue("County");
        comboBox9.setItems(comboBoxList);

        comboBox10.setValue("County");
        comboBox10.setItems(comboBoxList);

        comboBox11.setValue("County");
        comboBox11.setItems(comboBoxList);

        comboBox12.setValue("County");
        comboBox12.setItems(comboBoxList);
    }

        @FXML
        public void calculateTotal(ActionEvent event){

        //add values put into textfields  
       // calculateTotal.setOnAction(new EventHandler<ActionEvent>() {
       // if(event.getSource()==calculateTotal){

        //row1
        int sal1 = Integer.parseInt(sale1.getText());
        int taxx1 = Integer.parseInt(tax1.getText());
        int frei1 = Integer.parseInt(freight1.getText());
        int lab1 = Integer.parseInt(labor1.getText());
        int cos1 = Integer.parseInt(cost1.getText());        
        int result1 = sal1 + taxx1 + frei1 + lab1 + cos1;
        profit1.setText(profit1.getText()+result1);


        //row2
        int sal2 = Integer.parseInt(sale2.getText());
        int taxx2 = Integer.parseInt(tax2.getText());
        int frei2 = Integer.parseInt(freight2.getText());
        int lab2 = Integer.parseInt(labor2.getText());
        int cos2 = Integer.parseInt(cost2.getText());        
        int result2 = sal2 + taxx2 + frei2 + lab2 + cos2;
        profit2.setText(profit2.getText()+result2);     

        //row3
        int sal3 = Integer.parseInt(sale3.getText());
        int taxx3 = Integer.parseInt(tax3.getText());
        int frei3 = Integer.parseInt(freight3.getText());
        int lab3 = Integer.parseInt(labor3.getText());
        int cos3 = Integer.parseInt(cost3.getText());        
        int result3 = sal3 + taxx3 + frei3 + lab3 + cos3;
        profit3.setText(profit3.getText()+result3);

        //row4
        int sal4 = Integer.parseInt(sale4.getText());
        int taxx4 = Integer.parseInt(tax4.getText());
        int frei4 = Integer.parseInt(freight4.getText());
        int lab4 = Integer.parseInt(labor4.getText());
        int cos4 = Integer.parseInt(cost4.getText());        
        int result4 = sal4 + taxx4 + frei4 + lab4 + cos4;
        profit4.setText(profit4.getText()+result4);

        //row5
        int sal5 = Integer.parseInt(sale5.getText());
        int taxx5 = Integer.parseInt(tax5.getText());
        int frei5 = Integer.parseInt(freight5.getText());
        int lab5 = Integer.parseInt(labor5.getText());
        int cos5 = Integer.parseInt(cost5.getText());        
        int result5 = sal5 + taxx5 + frei5 + lab5 + cos5;
        profit5.setText(profit5.getText()+result5);

        //row6
        int sal6 = Integer.parseInt(sale6.getText());
        int taxx6 = Integer.parseInt(tax6.getText());
        int frei6 = Integer.parseInt(freight6.getText());
        int lab6 = Integer.parseInt(labor6.getText());
        int cos6 = Integer.parseInt(cost6.getText());        
        int result6 = sal6 + taxx6 + frei6 + lab6 + cos6;
        profit6.setText(profit6.getText()+result6);

        //row7
        int sal7 = Integer.parseInt(sale7.getText());
        int taxx7 = Integer.parseInt(tax7.getText());
        int frei7 = Integer.parseInt(freight7.getText());
        int lab7 = Integer.parseInt(labor7.getText());
        int cos7 = Integer.parseInt(cost7.getText());        
        int result7 = sal7 + taxx7 + frei7 + lab7 + cos7;
        profit7.setText(profit7.getText()+result7);

        //row8
        int sal8 = Integer.parseInt(sale8.getText());
        int taxx8 = Integer.parseInt(tax8.getText());
        int frei8 = Integer.parseInt(freight8.getText());
        int lab8 = Integer.parseInt(labor8.getText());
        int cos8 = Integer.parseInt(cost8.getText());        
        int result8 = sal8 + taxx8 + frei8 + lab8 + cos8;
        profit8.setText(profit8.getText()+result8);

        //row9
        int sal9 = Integer.parseInt(sale9.getText());
        int taxx9 = Integer.parseInt(tax9.getText());
        int frei9 = Integer.parseInt(freight9.getText());
        int lab9 = Integer.parseInt(labor9.getText());
        int cos9 = Integer.parseInt(cost9.getText());        
        int result9 = sal9 + taxx9 + frei9 + lab9 + cos9;
        profit9.setText(profit9.getText()+result9);

        //row10
        int sal10 = Integer.parseInt(sale10.getText());
        int taxx10 = Integer.parseInt(tax10.getText());
        int frei10 = Integer.parseInt(freight10.getText());
        int lab10 = Integer.parseInt(labor10.getText());
        int cos10 = Integer.parseInt(cost10.getText());        
        int result10 = sal10 + taxx10 + frei10 + lab10 + cos10;
        profit10.setText(profit10.getText()+result10);

        //row11
        int sal11 = Integer.parseInt(sale11.getText());
        int taxx11 = Integer.parseInt(tax11.getText());
        int frei11 = Integer.parseInt(freight11.getText());
        int lab11 = Integer.parseInt(labor11.getText());
        int cos11 = Integer.parseInt(cost11.getText());        
        int result11 = sal11 + taxx11 + frei11 + lab11 + cos11;
        profit11.setText(profit11.getText()+result11);

        //row12
        int sal12 = Integer.parseInt(sale12.getText());
        int taxx12 = Integer.parseInt(tax12.getText());
        int frei12 = Integer.parseInt(freight12.getText());
        int lab12 = Integer.parseInt(labor12.getText());
        int cos12 = Integer.parseInt(cost12.getText());        
        int result12 = sal12 + taxx12 + frei12 + lab12 + cos12;
        profit12.setText(profit12.getText()+result12);




        }


}

2 个答案:

答案 0 :(得分:1)

您正在获取空指针异常,因为您尚未初始化sale1,您只是将其声明为decclared。 为了更好地理解Null指针异常,请点击以下链接: https://stackoverflow.com/a/218510/3420973

答案 1 :(得分:-1)

我会假设您收到此错误,因为您使用getText()函数获取的文本无法转换为整数。

您可以在其周围添加try语句。

例如

try { //it will attempt to convert the text
... your code
} catch (Exception e) { // if it fails the following code will happen
... error response code
} 

编辑:

看来你的问题实际上并没有初始化TextFields。这是一篇可能有用How can a textfield from fxml file be updated by setText in java file?

的帖子

这是另一个How can I fix a NullPointerException when interacting with FXML elements in the start method?