gpt4 book ai didi

java - 使用 JavaFX set 和 get 方法时出现 InloggingTargetException

转载 作者:行者123 更新时间:2023-12-01 12:11:43 29 4
gpt4 key购买 nike

    //Max Maier, Milan Radovanovic
package application;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class Control {
private static int anz = 1;
int generierteZahl = (int) Math.ceil(Math.random()*100);
Label anzversuche;
Label zahlenbereich;
Button ratenb;
TextField eingabe;
@FXML void raten(){
if (generierteZahl == Integer.parseInt(eingabe.getText())){
anzversuche.setText(anz + ". Versuche");
ratenb.setDisable(true);
zahlenbereich.setText("Richtig geraten!!!");
}
else if (generierteZahl > Integer.parseInt(eingabe.getText())){
anz++;
anzversuche.setText(anz + ". Versuche");
zahlenbereich.setText(Integer.parseInt(eingabe.getText()) + " ist zu klein");
}
else{
anzversuche.setText(anz + ". Versuche");
zahlenbereich.setText(Integer.parseInt(eingabe.getText()) + " ist zu groß");
}
anz++;
}
@FXML void neuerVersuch(){
anz = 1;
generierteZahl = (int) Math.ceil(Math.random()*100);
zahlenbereich.setText("Noch keine Zahl eingegeben");
anzversuche.setText(anz + ". Versuche");
ratenb.setDisable(false);
eingabe.setText("");
}

}

这会抛出一堆异常:

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Node.fireEvent(Unknown Source)
at javafx.scene.control.Button.fire(Unknown Source)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEventImpl(Unknown Source)
at com.sun.javafx.event.EventUtil.fireEvent(Unknown Source)
at javafx.event.Event.fireEvent(Unknown Source)
at javafx.scene.Scene$MouseHandler.process(Unknown Source)
at javafx.scene.Scene$MouseHandler.access$1500(Unknown Source)
at javafx.scene.Scene.impl_processMouseEvent(Unknown Source)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.handleMouseEvent(Unknown Source)
at com.sun.glass.ui.View.notifyMouse(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 48 more
Caused by: java.lang.NullPointerException
at application.Control.neuerVersuch(Control.java:34)
... 57 more

这是一个小型猜数游戏,创建一个随机数,然后与文本字段“eingabe”中的字符串进行比较。我明白为什么有时会抛出 NumberFormatException,但调用异常是全新的,我真的很希望得到一些帮助。

最佳答案

异常在最后说:

Caused by: java.lang.NullPointerException
at application.Control.neuerVersuch(Control.java:34)

虽然我不知道它是哪一行,但调用 neuerVersuch 时会出现错误,并且它发现一个未初始化的字段,可能是 zahlenbereich

原因是该字段(可能还有其他字段)未初始化,因为它未使用 @FXML 进行注释。如果这些是您的 FXML 文件中的控件,并且它们有 fx:id,您应该对它们进行注释:

@FXML Label anzversuche;
@FXML Label zahlenbereich;
@FXML Button ratenb;
@FXML TextField eingabe;

因此,当加载 Controller 时,它们会被正确实例化。

关于java - 使用 JavaFX set 和 get 方法时出现 InloggingTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27233078/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com