У меня есть список японских блюд, и пользователь выбирает, какое блюдо будет коммандным. Я пытаюсь проверить, какие из Checkbox
es выбраны из thread , и распечатать цену в Label
, но когда я запускаю свой код, он возвращает NullPointerException
вот мой код:
int price=0;
@FXML private Label textPrice;
//Dishes
@FXML private CheckBox salade;
@FXML private CheckBox soupeMiso;
@FXML private CheckBox soupe_miso_EBI;
public void threadPrice() {
new Thread(new Runnable() {
@Override public void run() {
Platform.runLater(new Runnable() {
@Override public void run() {
//selectedSushi() ;
if(salade.isSelected()) {
System.out.println("salade selected");
price=price+Sushi.SALADE.getPrice();
}
if(soupeMiso.isSelected()) {
price=price+Sushi.SOUPE_MISOU.getPrice();
}
if(soupe_miso_EBI.isSelected()) {
price=price+Sushi.SOUPE_MISOU_EBI.getPrice();
}
}
});
}}).start();
String strPrice = Integer.toString(price);
textPrice.setText("price:"+strPrice);
}
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>
<AnchorPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MenuController">
<children>
<AnchorPane prefHeight="486.0" prefWidth="617.0">
<children>
<HBox layoutX="195.0" layoutY="10.0" prefHeight="49.0" prefWidth="193.0">
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="MENU" textAlignment="CENTER" wrappingWidth="195.6796875">
<font>
<Font size="52.0" />
</font>*
</Text>
</children>
</HBox>
<HBox layoutX="25.0" layoutY="160.0" prefHeight="26.0" prefWidth="578.0">
<children>
<CheckBox fx:id="soupeMiso" mnemonicParsing="false" text="Soupe Miso">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</CheckBox>
<CheckBox fx:id="salade1" mnemonicParsing="false" text="Salade">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</CheckBox>
<CheckBox fx:id="soupe_miso_EBI" mnemonicParsing="false" text="Supe Miso EBI">
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</CheckBox>
</children>
</HBox>
<HBox layoutX="25.0" layoutY="275.0" prefHeight="26.0" prefWidth="578.0" />
<Label fx:id="textPrice" layoutX="204.0" layoutY="393.0" prefHeight="44.0" prefWidth="196.0" text="price:">
<font>
<Font size="38.0" />
</font>
</Label>
<Button fx:id="order" layoutX="215.0" layoutY="358.0" mnemonicParsing="false" onAction="#selectedSushi" text="ORDER" />
</children>
</AnchorPane>
Ошибка:
Exception in thread "JavaFX Application Thread"
java.lang.NullPointerException
at application.MenuController$1$1.run(MenuController.java:143)
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.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:748)