使用JavaFX set和get方法时的InvocationTargetException [英] InvocationTargetException when using JavaFX set and get methods

查看:266
本文介绍了使用JavaFX set和get方法时的InvocationTargetException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    //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,但是调用异常是全新的,我真的很感激一些帮助。

It is a small number-guessing game where a random number is created and then compared with the String in the textfield "eingabe". I get why sometimes the NumberFormatException is thrown, but the invoke exception is completely new to and i really would appreciate some help.

推荐答案

最后的例外说:

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

虽然我不知道它是什么行,但错误来了当 neuerVersuch 被调用时,它会找到一个未初始化的字段,可能是 zahlenbereich

Though I can't tell what line is it, the error comes when neuerVersuch is called, and it finds an uninitialized field, probably zahlenbereich.

原因是这个字段(可能是其他字段)没有实例化,因为它没有用 @FXML 。如果thouse是你的FXML文件中的控件,并且他们有一个 fx:id ,你应该注释它们:

The reason for this is this field (and maybe the others) is not instatiated since it is not annotated with @FXML. If thouse are controls in your FXML file, and they have an fx:id, you should annotate them:

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

所以当加载控制器时,它们会被正确地即时显示。

so when the controller is loaded, they are properly instantited.

这篇关于使用JavaFX set和get方法时的InvocationTargetException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆