前提・実現したいこと
JavaFXでラベルにランダムに出力される数字とテキストフィールドに入力した値が一致しているかどうかを
チェックするプログラムを作っています。
ボタンがクリックされたときにランダムな数字とテキストフィールドの値をチェックし、正誤を別のラベルに出したいのですが、エラーが発生してしまいました。
調べたところ、似たような事例を見つけたのですがいまいち理解できません。
特にlabel_OutputはsetTextできて、correctLabelがsetTextできないのがわかりません
###環境
OS macOS Mojave 10.14.5
Eclipse Version: 2018-09 (4.9.0)
Scene Builder 2.0
e(fx)clipe
発生している問題・エラーメッセージ
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774) at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657) 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) Caused by: 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:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771) ... 45 more Caused by: java.lang.NullPointerException at application.FormController.onButtonClicked(FormController.java:37) ... 55 more
該当のソースコード
Main.java
Java
1package application; 2 3import javafx.application.Application; 4import javafx.fxml.FXMLLoader; 5import javafx.scene.Scene; 6import javafx.scene.layout.AnchorPane; 7import javafx.stage.Stage; 8 9 10public class Main extends Application { 11 @Override 12 public void start(Stage primaryStage) { 13 try { 14 AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Form.fxml")); 15 Scene scene = new Scene(root,400,400); 16 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); 17 primaryStage.setScene(scene); 18 primaryStage.show(); 19 } catch(Exception e) { 20 e.printStackTrace(); 21 } 22 } 23 24 public static void main(String[] args) { 25 launch(args); 26 } 27} 28
FormController.java
Java
1package application; 2 3import java.util.Random; 4 5import javafx.animation.KeyFrame; 6import javafx.animation.Timeline; 7import javafx.event.ActionEvent; 8import javafx.event.EventHandler; 9import javafx.fxml.FXML; 10import javafx.scene.control.Button; 11import javafx.scene.control.Label; 12import javafx.scene.control.TextField; 13import javafx.util.Duration; 14public class FormController{ 15 @FXML private TextField field_Name; 16 @FXML private Button button_Enter; 17 @FXML private Button startButton; 18 @FXML private Label label_Output; 19 @FXML private Label correctLabel; 20 private Random rnd = new Random(); 21 private Integer rndNum; 22 23 @FXML public void onStartClicked() { 24 Timeline timer = new Timeline(new KeyFrame(Duration.millis(5000), new EventHandler<ActionEvent>(){ 25 @Override 26 public void handle(ActionEvent event) { 27 rndNum = rnd.nextInt(9) + 1; 28 label_Output.setText(Integer.toString(rndNum)); 29 } 30 })); 31 timer.setCycleCount(10); 32 timer.play(); 33 } 34 @FXML public void onButtonClicked() { 35 if(field_Name.getText().equals(null)){ 36 correctLabel.setText("値を入力してください"); 37 }else if(field_Name.getText().equals(Integer.toString(rndNum))) { 38 correctLabel.setText("マル"); 39 }else if (!(field_Name.getText().equals(Integer.toString(rndNum)))) { 40 correctLabel.setText("バツ"); 41 } 42 } 43} 44
Form.fxml
fxml
1<?xml version="1.0" encoding="UTF-8"?> 2 3<?import javafx.scene.text.*?> 4<?import javafx.scene.control.*?> 5<?import java.lang.*?> 6<?import javafx.scene.layout.*?> 7<?import javafx.scene.layout.AnchorPane?> 8 9<AnchorPane prefHeight="400.0" prefWidth="400.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.FormController"> 10 <children> 11 <TextField fx:id="field_Name" layoutX="117.0" layoutY="187.0" /> 12 <Button fx:id="button_Enter" layoutX="284.0" layoutY="187.0" mnemonicParsing="false" onAction="#onButtonClicked" text="Enter" /> 13 <Label fx:id="label_Output" layoutX="15.0" layoutY="27.0" prefHeight="40.0" prefWidth="370.0" text="0"> 14 <font> 15 <Font name="System Bold" size="36.0" /> 16 </font></Label> 17 <Button fx:id="startButton" layoutX="171.0" layoutY="359.0" mnemonicParsing="false" onAction="#onStartClicked" text="Start" /> 18 <Label layoutX="15.0" layoutY="71.0" prefHeight="40.0" prefWidth="370.0" text="correct"> 19 <font> 20 <Font size="36.0" /> 21 </font></Label> 22 </children> 23</AnchorPane> 24
試したこと
以下のサイトを見ながら作成しておりました。
https://qiita.com/Kei_22/items/f54e6ba7cd4bf71b14c8
https://qiita.com/Kei_22/items/d3cf27a6d09560100145
https://www.xmisao.com/2014/09/25/javafx-timer.html
エラーが起きてから以下のサイトを調べましたがどのように記述すれば良いのかわかりません
一つ目のURLに書いてあることを自分が理解できていないことが原因だとは思うのですが、、、
https://teratail.com/questions/89165
https://stackoverflow.com/questions/22546028/label-settext-nullpointerexception
https://teratail.com/questions/65356
https://stackoverflow.com/questions/22546028/label-settext-nullpointerexception
初心者で申し訳ないのですがよろしくお願いします。
回答2件
あなたの回答
tips
プレビュー