Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help! My code is supposed to get 4 inputs from the GUI text box and displ

ID: 3770441 • Letter: P

Question

Please help!

My code is supposed to get 4 inputs from the GUI text box and display the running total and the number of entries as the user keeps on entering 4 inputs. For example, after entering 120 the user clicks "Enter Next Month Saving" button and enters the next month saving. After the first entry, total should equal first entry and the month counter 1. Then user inputs the second month saving. Program keeps on adding values entered and increment counter. Below is unfinished code.

import javafx.stage.Stage;
   import javafx.application.Application;
   import javafx.geometry.Pos;
   import javafx.scene.Scene;
   import javafx.scene.control.Button;
   import javafx.scene.control.Label;  
   import javafx.scene.layout.BorderPane;
   import javafx.scene.layout.GridPane;
   import javafx.scene.layout.HBox;
   import javafx.scene.control.TextField;
  
public class SumAndAverage extends Application{
                       
       private TextField tfSaving = new TextField();
       private TextField tfTotalSaving = new TextField();
       private TextField tfCountMonths = new TextField();

       
       public void start(Stage primaryStage){
                       
           GridPane pane = new GridPane();
          
           pane.setHgap(5);
           pane.setVgap(5);
           pane.add(new Label("Saving"), 0,0);
           pane.add(tfSaving, 1, 0);
           pane.add(new Label ("Total Saving"), 0,1);
           pane.add(tfTotalSaving, 1, 1);
           pane.add(new Label ("Month Counter"), 0,2);
           pane.add(tfCountMonths, 1, 2);
           pane.setAlignment(Pos.CENTER);
          
            Button btNextMonthSaving = new Button("Next Month Saving");
           HBox hBox = new HBox(5);
           hBox.getChildren().add(btNextMonthSaving);
           BorderPane bPane = new BorderPane();
           bPane.setTop(pane);
           bPane.setBottom(hBox);
           hBox.setAlignment(Pos.CENTER);
          
            Scene scene = new Scene(bPane, 270,200);
           primaryStage.setTitle("Savings");
           primaryStage.setScene(scene);
           primaryStage.show();
            
           btNextMonthSaving.setOnAction(e ->{      });
          
       }        
       }

   

Explanation / Answer

The entire code in one place:

public static void main(String[] args) {

        System.out.println("Test Savings ");
  
  
    } // main end

At the above code, the control went into the launchImpl.java code straight from the closing bracket

It never went to the start function given in the code in the question

The symbol variable primaryStage was not found - was it dclared?

Hence the following changes were made

the start function is commented out

the entire contents of the start function is moved in to the main function

the declaration and uinitialization is made as follows:

public static void main(String[] args) {
        Stage primaryStage = null;
        System.out.println("Test Savings month by month ");
        GridPane pane = new GridPane();
    // then the rest of the code as it was in the start function before

up to the end as follows:

Scene scene = new Scene(bPane, 270,200);
            primaryStage.setTitle("Savings");
            primaryStage.setScene(scene);
            primaryStage.show();
          
            btNextMonthSaving.setOnAction(e ->{       });
          
  
    } // main end
  
} // public class end

package sumandaverage;
import javafx.stage.Stage;
    import javafx.application.Application;
    import javafx.geometry.Pos;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;  
    import javafx.scene.layout.BorderPane;
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.control.TextField;

public abstract class SumAndAverage extends Application {   // since it does not override start it has to be abstract

    public static TextField tfSaving = new TextField();
    public static TextField tfTotalSaving = new TextField();
    public static TextField tfCountMonths = new TextField();
    private static Object primaryStage;

   // public void start(Stage primaryStage){
          
   // } // end of func start (stage primaryStage)       
  
    public static void main(String[] args) {
        Stage primaryStage = null;
        System.out.println("Test Savings month by month ");
        GridPane pane = new GridPane();
          
            pane.setHgap(5); // set the pane
            pane.setVgap(5);
            pane.add(new Label("Savings"), 0,0); // set the label as Savings
            pane.add(tfSaving, 1, 0);   // variable tfSaving is added to the pane
            pane.add(new Label ("Total Savings"), 0,1); // set label
            pane.add(tfTotalSaving, 1, 1);   // add the sum to pane
            pane.add(new Label ("Month Counter"), 0,2);
            pane.add(tfCountMonths, 1, 2); // add the month count to the pane
            pane.setAlignment(Pos.CENTER);
          
            Button btNextMonthSaving = new Button("Next Month Savings now"); // create button
            HBox hBox = new HBox(5); // box to hold 5 elements
            hBox.getChildren().add(btNextMonthSaving); // the children of the box holds next month savings
            BorderPane bPane = new BorderPane();
            bPane.setTop(pane);
            bPane.setBottom(hBox);
            hBox.setAlignment(Pos.CENTER);   // position at the center
          
            Scene scene = new Scene(bPane, 270,200);
            primaryStage.setTitle("Savings");
            primaryStage.setScene(scene);
            primaryStage.show(); // finally show off
          
            btNextMonthSaving.setOnAction(e ->{       });
          
  
    } // main end
  
} // public class end

run:
Test Savings month by month
java.lang.reflect.InvocationTargetException
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
   at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.NullPointerException
   at sumandaverage.SumAndAverage.main(SumAndAverage.java:48)
   ... 11 more
Exception running application sumandaverage.SumAndAverage
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)

        Stage primaryStage5;
        primaryStage5 = null;

        Stage primaryStage5;
        primaryStage5 = {" ", ' ', 0.0, 0, " "}

// as long as the Stage class has these data members String, char, fllat, int, and char in the same order