I'm writing a program in Java (a mortgage calculator) with a GUI using SceneBuilder/FXML for a college class and I'm experiencing errors

huangapple go评论109阅读模式
英文:

I'm writing a program in Java (a mortgage calculator) with a GUI using SceneBuilder/FXML for a college class and I'm experiencing errors

问题

I'm still quite a beginner in Java and I'm having a hard time understanding stuff like this program. However, I thought I got a hold of it eventually. Unfortunately, whenever I try to run my program, there are errors and I'm unsure how to fix them.

EDIT: I should also state that I need the slider to go between the values 10, 20, and 30.

Here's my controller program:

  1. // Controller that handles calculateButton and loanDurationSlider events
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.NumberFormat;
  5. import javafx.beans.value.ChangeListener;
  6. import javafx.beans.value.ObservableValue;
  7. import javafx.event.ActionEvent;
  8. import javafx.fxml.FXML;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.Slider;
  11. import javafx.scene.control.TextField;
  12. public class MortgageCalculatorController {
  13. // formatters for currency and percentages
  14. private static final NumberFormat currency =
  15. NumberFormat.getCurrencyInstance();
  16. private static final NumberFormat percent =
  17. NumberFormat.getPercentInstance();
  18. // GUI controls defined in FXML and used by the controller's code
  19. @FXML
  20. private TextField purchasePriceTextField;
  21. @FXML
  22. private Label loanDurationLabel;
  23. @FXML
  24. private Slider loanDurationSlider;
  25. @FXML
  26. private TextField loanDurationTextField;
  27. @FXML
  28. private TextField downPaymentTextField;
  29. @FXML
  30. private TextField interestRateTextField;
  31. @FXML
  32. private TextField monthlyPaymentTextField;
  33. private int loanDuration = Integer.parseInt(loanDurationTextField.getText());
  34. @FXML
  35. private void calculateButtonPressed(ActionEvent event) {
  36. try {
  37. BigDecimal purchasePrice = new BigDecimal(purchasePriceTextField.getText());
  38. BigDecimal downPayment = new BigDecimal(downPaymentTextField.getText());
  39. BigDecimal interestRate = new BigDecimal(interestRateTextField.getText());
  40. BigDecimal twelve = new BigDecimal(12.0);
  41. BigDecimal hundred = new BigDecimal(100.0);
  42. interestRate = interestRate.divide(twelve);
  43. BigDecimal numerator = interestRate.multiply(interestRate.add(BigDecimal.ONE).pow(loanDuration*12));
  44. BigDecimal denominator = interestRate.add(BigDecimal.ONE).pow(loanDuration*12).subtract(BigDecimal.ONE);
  45. BigDecimal monthlyPayment = purchasePrice.multiply(numerator).divide(denominator);
  46. interestRate = interestRate.multiply(hundred);
  47. purchasePriceTextField.setText(currency.format(purchasePrice));
  48. downPaymentTextField.setText(currency.format(downPayment));
  49. interestRateTextField.setText(percent.format(interestRate));
  50. monthlyPaymentTextField.setText(currency.format(monthlyPayment));
  51. }
  52. catch (NumberFormatException ex) {
  53. purchasePriceTextField.setText("Enter amount");
  54. purchasePriceTextField.selectAll();
  55. purchasePriceTextField.requestFocus();
  56. }
  57. }
  58. // called by FXMLLoader to initialize the controller
  59. public void initialize() {
  60. // 0-4 rounds down, 5-9 rounds up
  61. currency.setRoundingMode(RoundingMode.HALF_UP);
  62. // listener for changes to loanDurationSlider's value
  63. loanDurationSlider.valueProperty().addListener(
  64. new ChangeListener<Number>() {
  65. @Override
  66. public void changed(ObservableValue<? extends Number> ov,
  67. Number oldValue, Number newValue) {
  68. loanDuration = newValue.intValue();
  69. }
  70. }
  71. );
  72. }
  73. }

And here's my FXML program to go with it:

  1. <!-- FXML content -->

And finally here's the list of errors:

  1. Exception in Application start method
  2. java.lang.reflect.InvocationTargetException
  3. ...
  4. Caused by: java.lang.RuntimeException: Exception in Application start method
  5. ...
  6. Caused by: javafx.fxml.LoadException:
  7. ...
  8. Caused by: java.lang.NullPointerException
  9. ...
  10. Exception running application MortgageCalculator

Please let me know if I need to supply any further information.

英文:

I'm still quite a beginner in Java and I'm having a hard time understanding stuff like this program. However, I thought I got a hold of it eventually. Unfortunately, whenever I try to run my program, there are errors and I'm unsure how to fix them.

EDIT: I should also state that I need the slider to go between the values 10, 20, and 30.

Here's my controller program:

  1. // Controller that handles calculateButton and loanDurationSlider events
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.NumberFormat;
  5. import javafx.beans.value.ChangeListener;
  6. import javafx.beans.value.ObservableValue;
  7. import javafx.event.ActionEvent;
  8. import javafx.fxml.FXML;
  9. import javafx.scene.control.Label;
  10. import javafx.scene.control.Slider;
  11. import javafx.scene.control.TextField;
  12. public class MortgageCalculatorController {
  13. // formatters for currency and percentages
  14. private static final NumberFormat currency =
  15. NumberFormat.getCurrencyInstance();
  16. private static final NumberFormat percent =
  17. NumberFormat.getPercentInstance();
  18. // GUI controls defined in FXML and used by the controller&#39;s code
  19. @FXML
  20. private TextField purchasePriceTextField;
  21. @FXML
  22. private Label loanDurationLabel;
  23. @FXML
  24. private Slider loanDurationSlider;
  25. @FXML
  26. private TextField loanDurationTextField;
  27. @FXML
  28. private TextField downPaymentTextField;
  29. @FXML
  30. private TextField interestRateTextField;
  31. @FXML
  32. private TextField monthlyPaymentTextField;
  33. private int loanDuration = Integer.parseInt(loanDurationTextField.getText());
  34. @FXML
  35. private void calculateButtonPressed(ActionEvent event) {
  36. try {
  37. BigDecimal purchasePrice = new BigDecimal(purchasePriceTextField.getText());
  38. BigDecimal downPayment = new BigDecimal(downPaymentTextField.getText());
  39. BigDecimal interestRate = new BigDecimal(interestRateTextField.getText());
  40. BigDecimal twelve = new BigDecimal(12.0);
  41. BigDecimal hundred = new BigDecimal(100.0);
  42. interestRate = interestRate.divide(twelve);
  43. BigDecimal numerator = interestRate.multiply(interestRate.add(BigDecimal.ONE).pow(loanDuration*12));
  44. BigDecimal denominator = interestRate.add(BigDecimal.ONE).pow(loanDuration*12).subtract(BigDecimal.ONE);
  45. BigDecimal monthlyPayment = purchasePrice.multiply(numerator).divide(denominator);
  46. interestRate = interestRate.multiply(hundred);
  47. purchasePriceTextField.setText(currency.format(purchasePrice));
  48. downPaymentTextField.setText(currency.format(downPayment));
  49. interestRateTextField.setText(percent.format(interestRate));
  50. monthlyPaymentTextField.setText(currency.format(monthlyPayment));
  51. }
  52. catch (NumberFormatException ex) {
  53. purchasePriceTextField.setText(&quot;Enter amount&quot;);
  54. purchasePriceTextField.selectAll();
  55. purchasePriceTextField.requestFocus();
  56. }
  57. }
  58. // called by FXMLLoader to initialize the controller
  59. public void initialize() {
  60. // 0-4 rounds down, 5-9 rounds up
  61. currency.setRoundingMode(RoundingMode.HALF_UP);
  62. // listener for changes to loanDurationSlider&#39;s value
  63. loanDurationSlider.valueProperty().addListener(
  64. new ChangeListener&lt;Number&gt;() {
  65. @Override
  66. public void changed(ObservableValue&lt;? extends Number&gt; ov,
  67. Number oldValue, Number newValue) {
  68. loanDuration = newValue.intValue();
  69. }
  70. }
  71. );
  72. }
  73. }

And here's my FXML program to go with it:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;?import javafx.geometry.Insets?&gt;
  3. &lt;?import javafx.scene.control.Button?&gt;
  4. &lt;?import javafx.scene.control.Label?&gt;
  5. &lt;?import javafx.scene.control.Slider?&gt;
  6. &lt;?import javafx.scene.control.TextField?&gt;
  7. &lt;?import javafx.scene.layout.ColumnConstraints?&gt;
  8. &lt;?import javafx.scene.layout.GridPane?&gt;
  9. &lt;?import javafx.scene.layout.RowConstraints?&gt;
  10. &lt;GridPane hgap=&quot;8.0&quot; maxHeight=&quot;-Infinity&quot; maxWidth=&quot;-Infinity&quot; minHeight=&quot;-Infinity&quot; minWidth=&quot;-Infinity&quot; xmlns=&quot;http://javafx.com/javafx/8.0.60&quot; xmlns:fx=&quot;http://javafx.com/fxml/1&quot; fx:controller=&quot;MortgageCalculatorController&quot;&gt;
  11. &lt;columnConstraints&gt;
  12. &lt;ColumnConstraints halignment=&quot;RIGHT&quot; hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; /&gt;
  13. &lt;ColumnConstraints hgrow=&quot;SOMETIMES&quot; minWidth=&quot;10.0&quot; /&gt;
  14. &lt;/columnConstraints&gt;
  15. &lt;rowConstraints&gt;
  16. &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
  17. &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
  18. &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
  19. &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
  20. &lt;RowConstraints minHeight=&quot;10.0&quot; prefHeight=&quot;30.0&quot; vgrow=&quot;SOMETIMES&quot; /&gt;
  21. &lt;/rowConstraints&gt;
  22. &lt;children&gt;
  23. &lt;Label text=&quot;Purchase Price&quot; /&gt;
  24. &lt;Label fx:id=&quot;loanDurationLabel&quot; text=&quot;10&quot; GridPane.rowIndex=&quot;1&quot; /&gt;
  25. &lt;Label text=&quot;Loan Duration&quot; GridPane.rowIndex=&quot;2&quot; /&gt;
  26. &lt;Label text=&quot;Down Payment&quot; GridPane.rowIndex=&quot;3&quot; /&gt;
  27. &lt;Label text=&quot;Interest Rate&quot; GridPane.rowIndex=&quot;4&quot; /&gt;
  28. &lt;Label text=&quot;Monthly Payment&quot; GridPane.rowIndex=&quot;5&quot; /&gt;
  29. &lt;TextField fx:id=&quot;purchasePriceTextField&quot; GridPane.columnIndex=&quot;1&quot; /&gt;
  30. &lt;TextField fx:id=&quot;loanDurationTextField&quot; editable=&quot;false&quot; focusTraversable=&quot;false&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;2&quot; /&gt;
  31. &lt;TextField fx:id=&quot;downPaymentTextField&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;3&quot; /&gt;
  32. &lt;TextField fx:id=&quot;interestRateTextField&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;4&quot; /&gt;
  33. &lt;TextField fx:id=&quot;monthlyPaymentTextField&quot; editable=&quot;false&quot; focusTraversable=&quot;false&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;5&quot; /&gt;
  34. &lt;Slider fx:id=&quot;loanDurationSlider&quot; blockIncrement=&quot;10.0&quot; max=&quot;30.0&quot; value=&quot;10.0&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;1&quot; /&gt;
  35. &lt;Button maxWidth=&quot;1.7976931348623157E308&quot; mnemonicParsing=&quot;false&quot; onAction=&quot;#calculateButtonPressed&quot; text=&quot;Calculate&quot; GridPane.columnIndex=&quot;1&quot; GridPane.rowIndex=&quot;6&quot; /&gt;
  36. &lt;/children&gt;
  37. &lt;padding&gt;
  38. &lt;Insets bottom=&quot;14.0&quot; left=&quot;14.0&quot; right=&quot;14.0&quot; top=&quot;14.0&quot; /&gt;
  39. &lt;/padding&gt;
  40. &lt;/GridPane&gt;

And finally here's the list of errors:

  1. Exception in Application start method
  2. java.lang.reflect.InvocationTargetException
  3. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  4. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  5. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  6. at java.lang.reflect.Method.invoke(Method.java:498)
  7. at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
  8. at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
  9. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  10. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  11. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  12. at java.lang.reflect.Method.invoke(Method.java:498)
  13. at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:873)
  14. Caused by: java.lang.RuntimeException: Exception in Application start method
  15. at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
  16. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
  17. at java.lang.Thread.run(Thread.java:748)
  18. Caused by: javafx.fxml.LoadException:
  19. /C:/Users/EdenFlorian/Documents/Eclipse%20Workspace/COMP%20SCI%20316%20-%20Week%205%20Assignment/bin/MortgageCalculator.fxml:12
  20. at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
  21. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
  22. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
  23. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
  24. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
  25. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
  26. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
  27. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
  28. at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
  29. at MortgageCalculator.start(MortgageCalculator.java:13)
  30. at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
  31. at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
  32. at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
  33. at java.security.AccessController.doPrivileged(Native Method)
  34. at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
  35. at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
  36. at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
  37. at com.sun.glass.ui.win.WinApplication.lambda$null$4(WinApplication.java:186)
  38. ... 1 more
  39. Caused by: java.lang.NullPointerException
  40. at MortgageCalculatorController.&lt;init&gt;(MortgageCalculatorController.java:43)
  41. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  42. at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
  43. at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
  44. at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  45. at java.lang.Class.newInstance(Class.java:442)
  46. at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
  47. at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
  48. at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
  49. at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
  50. at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
  51. at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
  52. at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
  53. ... 17 more
  54. Exception running application MortgageCalculator

Please let me know if I need to supply any further information.

答案1

得分: 1

尝试去掉以下代码:

  1. private int loanDuration = Integer.parseInt(loanDurationTextField.getText());

然后将以下代码段替换为:

  1. BigDecimal numerator = interestRate.multiply(interestRate.add(BigDecimal.ONE).pow(Integer.parseInt(loanDurationTextField.getText())*12));
  2. BigDecimal denominator = interestRate.add(BigDecimal.ONE).pow(Integer.parseInt(loanDurationTextField.getText())*12).subtract(BigDecimal.ONE);

更新以回应评论
你应该可以像这样进行更新:

  1. loanDurationSlider.valueProperty().addListener(
  2. new ChangeListener<Number>() {
  3. @Override
  4. public void changed(ObservableValue<? extends Number> ov, Number oldValue, Number newValue)
  5. {
  6. loanDurationTextField.setText(newValue.intValue().toString())); //或者如果 intValue().toString() 不可用,可以使用 loanDurationTextField.setText(Integer.toString(newValue.intValue()));。
  7. }
  8. }
  9. );
英文:

Try getting rid of

  1. private int loanDuration = Integer.parseInt(loanDurationTextField.getText());

Then replace

  1. BigDecimal numerator = interestRate.multiply(interestRate.add(BigDecimal.ONE).pow(loanDuration*12));
  2. BigDecimal denominator = interestRate.add(BigDecimal.ONE).pow(loanDuration*12).subtract(BigDecimal.ONE);

with

  1. BigDecimal numerator = interestRate.multiply(interestRate.add(BigDecimal.ONE).pow(Integer.parseInt(loanDurationTextField.getText())*12));
  2. BigDecimal denominator = interestRate.add(BigDecimal.ONE).pow(Integer.parseInt(loanDurationTextField.getText())*12).subtract(BigDecimal.ONE);

Update to answer comment
You should be able to do something like

  1. loanDurationSlider.valueProperty().addListener(
  2. new ChangeListener&lt;Number&gt;() {
  3. @Override
  4. public void changed(ObservableValue&lt;? extends Number&gt; ov, Number oldValue, Number newValue)
  5. {
  6. loanDurationTextField.setText(newValue.intValue().toString()));//or loanDurationTextField.setText(Integer.toString(newValue.intValue())); if intValue().toString() is not valid.
  7. }
  8. }
  9. );

huangapple
  • 本文由 发表于 2020年10月9日 04:33:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64270225.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定