Reputation: 45
i am currently working on an app that calculates the monthly payment of a loan. ive taken code from my other programs to make what i want and i believe i have everything i need to execute it but the build fails and im not sure why. Javafx is something i am new to so i am very looking to learn more about it but could use some direction to my errors
i have tried going line by line with previous programs but dont see any differences so unsure why the error. also have has a classmate look at it to help debug
package project2a;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TitledPane;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
/**
*
* @author Carmine , Eric , Rohit
*/
public class Project2a extends Application {
//Text fields for loan amount
TextField tfLoanAmount = new TextField();
TextField tfInterestRate= new TextField();
TextArea textArea = new TextArea();
//Radio buttons for years
private RadioButton rbOneYear;
private RadioButton rbTwoYear;
private RadioButton rbFiveYear;
private RadioButton rbSevenYear;
private RadioButton rbTenYear;
private RadioButton rbFifteenYear;
private ToggleGroup group;
private Button btnCalculate;
//labels for output
private Label lblMonthlyPayment;
private Label lblHeader;
private Label lblYears;
//vbox for layout of items
private VBox vBoxRadio;
private VBox vBoxInputs;
private VBox vBoxResults;
private TitledPane titledPaneRadio;
@Override
public void start(Stage primaryStage)
{
//makes header
lblHeader= new Label("Calculate your monthly loan payment!");
lblHeader.setMinWidth(350);
lblHeader.setAlignment(Pos.CENTER);
lblHeader.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 28px; -fx-text-fill: white; -fx-background-color: rgb(104, 50, 0); -fx-font-weight: bold;");
EventHandler<ActionEvent> handler = event -> updateRates();
tfLoanAmount.setOnAction(handler);
tfInterestRate.setOnAction(handler);
TextField tfLoanAmount = new TextField();
rbOneYear= new RadioButton("One Years");
rbTwoYear= new RadioButton("Two Years");
rbFiveYear= new RadioButton("Five Years");
rbSevenYear= new RadioButton("Seven Years");
rbTenYear= new RadioButton("Ten Years");
rbFifteenYear= new RadioButton("Fifteen Years");
group = new ToggleGroup();
group.getToggles().addAll(rbOneYear, rbTwoYear, rbFiveYear, rbSevenYear, rbTenYear, rbFifteenYear);
rbFiveYear.setSelected(true);
vBoxRadio = new VBox(20,rbOneYear, rbTwoYear, rbFiveYear, rbSevenYear, rbTenYear, rbFifteenYear);
vBoxRadio.setPadding( new Insets(10) );
titledPaneRadio = new TitledPane("Number of Years", vBoxRadio);
vBoxInputs = new VBox(0, titledPaneRadio);
//creates calculate button
btnCalculate = new Button(" Calculate ");
btnCalculate.setStyle("-fx-text-fill: white; -fx-background-color: tan; -fx-font-weight: bold;");
btnCalculate.setOnAction( e -> calculateMonthlyPayment(e) );
lblYears= new Label("Number of Years: \t");
vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);
vBoxResults.setPrefWidth(140);
vBoxResults.setPrefHeight(270);
vBoxResults.setPadding( new Insets(10) );
vBoxResults.setStyle("-fx-background-color: cornsilk; -fx-border-color: black; -fx-border-radius: 10;");
GridPane grid = new GridPane();
grid.addRow(0, lblHeader);
GridPane.setColumnSpan(lblHeader, 2);
GridPane.setHalignment(lblHeader, HPos.CENTER);
grid.addRow(1, vBoxInputs, vBoxResults);
Scene scene = new Scene(grid, 350, 310);
primaryStage.setTitle("Loan Payment Calculator");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
private void updateRates() {
double annualInterestRate = 5.00;
double loanAmount = Double.parseDouble(tfLoanAmount.getText());
double numberOfYears = Double.parseDouble(lblYears.getText());
String s = String.format("%-1s%20s%20s\n", "Interest Rate", "Monthly Payment", "Total Payment");
// making loop to display different interest rates
for ( ; annualInterestRate <= 8.00; annualInterestRate += 0.125) {
// calculating monthly and total payment rates
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
// making % string for formatting reasons...
String str = "%";
// Displaying formatted info
s += String.format("%-1.3f%s%19.2f%30.2f \n", annualInterestRate, str, ((int) (monthlyPayment * 100) / 100.0), ((int) (totalPayment * 100) / 100.0));
}
textArea.setText(s);
}
private void calculateMonthlyPayment(ActionEvent e)
{
int years=0;
if(rbOneYear.isSelected())
{
years=1;
}
else if(rbTwoYear.isSelected())
{
years=2;
}
else if(rbFiveYear.isSelected())
{
years=5;
}
else if (rbSevenYear.isSelected())
{
years=7;
}
else if(rbTenYear.isSelected())
{
years=10;
}
else if(rbTenYear.isSelected())
{
years=10;
}
lblYears.setText("Year(s) Selected: " + years);
lblMonthlyPayment.setText("Monthly Payment: " );
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
what is expected is 6 radio buttons for years of the loan, the years displayed in a label on a side pannel, a text field to enter the loan amount and interest rate for the user to input and it be displayed on the side panel as well as used for calculations. and also some color and such to make it look nice for users
Upvotes: 1
Views: 62
Reputation: 1260
Exception is throwing in vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);
your issue is adding not initialized (null) lblMonthlyPayment
into VBox ,
Therefore initialize lblMonthlyPayment
and that will fix your app and it will run without a problem
Refer below,
lblYears = new Label("Number of Years: \t");
lblMonthlyPayment = new Label("");
vBoxResults = new VBox(50, lblMonthlyPayment, btnCalculate);
Upvotes: 1