每当我做一个动作,我都会得到这个
Button[id=gButton, styleClass=button]'Guess'
gField content: 44
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1762)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1645)
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:8216)
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:3724)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3452)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1728)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2461)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:348)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:273)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:382)
at com.sun.glass.ui.View.handleMouseEvent(View.java:553)
at com.sun.glass.ui.View.notifyMouse(View.java:925)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/728890494.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
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:483)
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:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1757)
... 47 more
Caused by: java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at javafxapplication.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:44)
... 57 more
注意
NumberFormatException for input string: ""
-您可以在第2行看到我的输入,gField content: 44
这是我的
FXMLDocumentController.java
:private Label gLabel;
@FXML
private Button rButton;
@FXML
private Button gButton;
@FXML
private SubScene colorBar;
@FXML
private TextField gField;
@FXML
private void handleButtonAction(ActionEvent event) {
Object source = event.getSource();
String guess = gField.getText();
System.out.println("You clicked me!");
System.out.println(source);
if (source == gButton || source == gButton) {
if (Integer.parseInt(guess) > JavaFXApplication.nToGuess) {
JavaFXApplication.nTries++;
gLabel.setText("Too High!");
} else if (Integer.parseInt(guess) < JavaFXApplication.nToGuess) {
JavaFXApplication.nTries++;
gLabel.setText("Too Low!");
} else if (Integer.parseInt(guess) == JavaFXApplication.nToGuess) {
if (JavaFXApplication.nTries > 1) {
gLabel.setText("Took you " + JavaFXApplication.nTries + " tries. Restarted.");
System.out.println("Took you " + JavaFXApplication.nTries + " tries. Restarted.");
} else if (JavaFXApplication.nTries == 0) {
gLabel.setText("You are a hacker.");
System.out.println("You are a hacker.");
} else if (JavaFXApplication.nTries == 1) {
gLabel.setText("It's taken you only 1 try.");
System.out.println("It's taken you only 1 try.");
}
JavaFXApplication.nTries = 0;
JavaFXApplication.rand = new Random();
JavaFXApplication.nToGuess = JavaFXApplication.rand.nextInt(100);
System.out.println(JavaFXApplication.nToGuess);
}
} else if (source == rButton) {
JavaFXApplication.nTries = 0;
gLabel.setText("Game has been restarted.");
JavaFXApplication.rand = new Random();
JavaFXApplication.nToGuess = JavaFXApplication.rand.nextInt(100);
System.out.println("Game restarted.\n" + JavaFXApplication.nToGuess);
}
gField.setText(null);
}
@Override
public void initialize(URL url, ResourceBundle rb) {
assert gButton != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
assert rButton != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
assert colorBar != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
assert gLabel != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
assert gField != null : "fx:id=\"myButton\" was not injected: check your FXML file 'simple.fxml'.";
}
这是我的
JavaFXApplication.java
:static int nTries = 0;
static Random rand = new Random();
static int nToGuess = rand.nextInt(100);
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
public static String nTriesTextGetter() {
return Integer.toString(nTries);
}
我知道在摇摆中,您必须使用动作事件侦听器和
EventQueue.invokeLater
,以便逻辑不会在EDT线程中运行,这是我使用JavaFX的第一个应用程序。任何解释都会有很大帮助。 最佳答案
如果您超过Integer的大小限制,您的String也将包含空值,则会出现这种类型的错误。
关于java - 奇怪的Java FXML NumberFormatException?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26081495/