因此,到目前为止,到目前为止,我实际上从未遇到过编码方面的麻烦。我正在为一个班级项目制作一个计算器,它将简单地将与组合框中的单词相对应的两个数字相加。我设置了数组,以便可以使用它们的索引轻松地将它们加在一起,并将总数显示在我的底部文本框中。我的代码将编译,但是我始终会收到错误消息,无论我进行什么查找,都找不到问题。我不知道我是否正确地获得了索引,我也不确定从组合框返回索引的另一种方法。任何帮助将不胜感激,谢谢您。
这是我的代码:
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.layout.GridPane;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.control.TextField;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
public class Dorm extends Application
{
private Label iMessage; // displays instructions
private TextField tDorms; // displays text to choose dormitories
private TextField tMeals; // display the text to choose a meal plan
private Label tTotal; // will display the total cost of tution.
private ComboBox cDormitories; // combo box for dormitories
private ComboBox cMeals; // combo box for meal plans;
private VBox mealStuff;// holds objects for the meal plans
private VBox dormStuff; // holds objects for the dormitories.
private Button getAnswer; // will perform the multiplication of the chosen dormitory and meal plan. maybe not needed.
private GridPane everything; // will hold everything and be sent to scene
private int mIndex, dIndex;
// Dormitory Array
private String[] aDorm = {"Allen Hall $ 1800", "Pike Hall $ 2200", "Farthing Hall $ 2800", "University Suites $ 3000"};
// Dorm value array
int[] dormRate = {1800, 2200, 2800, 3000};
// Meal Plan Array
private String[] aMeal =
{
"7 Meals Per Week $ 600",
"14 Meals Per Week $ 1100",
"Unlimited Meals $ 1800" };
// Meal Plan Value Array
int[] mealRate = {600, 1100, 1800};
public static void main (String [] args)
{
launch (args);
}
@Override
public void start(Stage primaryStage)
{
// Create a label for instructions
Label iMessage = new Label("Instructions: Please select each option to find out your total cost: ");
// create the dormitories combobox and controls. then addd them to the VBox
// comboBox
ComboBox<String> cDormitories = new ComboBox<>();
cDormitories.getItems().addAll(aDorm);
cDormitories.setVisibleRowCount(4);
// comboBox label
Label tDorms = new Label("Select a Dormitory");
// add to VBox
VBox dormStuff = new VBox(10, cDormitories, tDorms);
dormStuff.setPadding(new Insets(10));
dormStuff.setAlignment(Pos.CENTER);
//create the meal plan combo box, add controls, and add to a VBov.
ComboBox<String> cMeals = new ComboBox<>();
cMeals.getItems().addAll(aMeal);
cMeals.setVisibleRowCount(3);
// comboBox label
Label tMeals = new Label("Select a Meal Plan");
// add to VBox
VBox mealStuff = new VBox(10, cMeals, tMeals);
mealStuff.setPadding(new Insets(10));
mealStuff.setAlignment(Pos.CENTER);
//create a label for the total
Label tTotal = new Label("Your total is: "); //+ total ); answer needs to go here
// create a button
Button getAnswer = new Button("Get Answer");
// register event for the button
getAnswer.setOnAction(new DormComboButton());
// add both VBoxs to a grid pane
// create the grid pane
GridPane everything = new GridPane();
everything.add(iMessage, 1,0);
everything.add(dormStuff,0,1);
everything.add(mealStuff,2,1);
everything.add(tTotal,1,2);
everything.add(getAnswer,0,2);
// Create a scene and display it
Scene scene = new Scene(everything);
primaryStage.setScene(scene);
primaryStage.setTitle("Dorm and Meal Calculator");
primaryStage.show();
}
class DormComboButton implements EventHandler<ActionEvent>
{
@Override
public void handle(ActionEvent event)
{
//get the dorm index
int dIndex = cDormitories.getSelectionModel().getSelectedIndex(); //suppose to retrieve index from selected item in combo box.
//get the meal index
int mIndex = cMeals.getSelectionModel().getSelectedIndex(); //suppose to retrieve index from selected item in combo box.
//add those numbers together
int t = dormRate[dIndex] + mealRate[mIndex]; // should store the math
String str = Integer.toString(t); // turn math into a string
//display the results
tTotal.setText(str); //place math into the label
}
}
}
[当我按下“获取答案”按钮时收到运行时错误。] [1]
最新堆栈跟踪:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at Dorm$DormComboButton.handle(Dorm.java:120)
at Dorm$DormComboButton.handle(Dorm.java:114)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
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(Unknown Source)