Java中的简单统计平均值计算

时间:2019-06-19 00:12:23

标签: java exception javafx

我试图制作一个javafx程序,该程序将击中次数和击球时间分开以得到击球平均值。我遇到了很多异常,但我似乎无法找出正在发生的事情

我尝试使用BorderPane,但不起作用。

import javafx.application.Application;

import javafx.event.ActionEvent;

import javafx.event.EventHandler;

import javafx.scene.control.TextField;

import javafx.scene.control.Label;

import javafx.scene.Scene;

import javafx.scene.control.Button;

import javafx.scene.layout.VBox;

import javafx.scene.layout.BorderPane;

import javafx.stage.Stage;

public class BattingAvgCalc extends Application
{
   private TextField tfHits;

   private TextField tfTimesAtsBat;

   private TextField tfBattingAverage;

   private Label lbHits;

   private Label lbTimesAtsBat;

   private Label lbBattingAverage;

   private Button btCalc;

   private double dbHits = 0;

   private double dbTimesAtsBat = 0;

   private double dbBattingAverage = 0;

   @Override
   public void start(Stage primaryStage) throws Exception
   {
         primaryStage.setTitle("Pedro's Batting Average Calculator");

         //Name button set defaults values
         btCalc = new Button("Calculate");

         tfHits.setText(Double.toString(dbHits));
         tfTimesAtsBat.setText(Double.toString(dbTimesAtsBat));
         tfBattingAverage.setText(Double.toString(dbBattingAverage));

         //Calc Avg  
         btCalc.setOnAction(e -> 
         {
               dbHits = Double.parseDouble(tfHits.getText());
               dbTimesAtsBat = Double.parseDouble(tfHits.getText());

               dbBattingAverage = dbHits / dbTimesAtsBat;

               tfBattingAverage.setText(Double.toString(dbBattingAverage));

         });

         //Create VBoxs and BorderPane
         VBox vLabels = new VBox(5);
         vLabels.getChildren().addAll(lbHits,lbTimesAtsBat,lbBattingAverage);

         VBox vTextFields = new VBox(5);
         vTextFields.getChildren().addAll(tfHits,tfTimesAtsBat,tfBattingAverage);

         BorderPane bp = new BorderPane();
         bp.setCenter(vLabels);
         bp.setCenter(vTextFields);

         Scene scene = new Scene(bp,500,500);
         primaryStage.setScene(scene);
         primaryStage.show();
   }

}

我原本希望看到一个窗口,但相反,我遇到了一堆异常情况

编辑2:try块中的StackTraces

java.lang.NullPointerException
    at BattingAvgCalc.start(BattingAvgCalc.java:35)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案