事件调用时出现 javafx 错误

javafx error when event invokes

我是 javafx 的新手,刚开始,在按钮上调用事件时 运行 遇到一堆错误。我在带有 jdk1.8 的 NetBeans 8.1 中使用 SceneBuilder 2.0。 我在场景生成器中创建了基本布局

我在我的控制器 class 中创建了一个简单的方法,它只是从 textField 中获取文本并将其设置到 textArea 中。我把它设置在一个按钮上。 程序运行和构建都很好,但是当我点击按钮时出现错误。

这是我的代码:

package javafxapp;
import java.net.URL;
import java.lang.*;
import java.util.*;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
public class FXMLDocumentController implements Initializable {

@FXML
private TextField chatText;
@FXML
private TextArea chatArea;
@FXML
private void handleButtonAction(ActionEvent event) {
    System.out.println("You clicked me!");

}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    

public void sendMsg(ActionEvent action)
{
chatArea.setText(chatText.getText());
chatText.setText("");
}

}

这是我的主要内容 class:

package javafxapp;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;


public class JavaFXApp extends Application {

@Override
public void start(Stage stage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("/fxml/FXMLDocument.fxml"));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
}


public static void main(String[] args) {
    launch(args);
}

}

现在是错误:

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.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.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.handle(BehaviorSkinBase.java:96)
 at com.sun.javafx.scene.control.skin.BehaviorSkinBase.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.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.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.access00(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$null1(WinApplication.java:102)
 at com.sun.glass.ui.win.WinApplication$$Lambda/1147985808.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)
 ... 63 more
Caused by: java.lang.NullPointerException
 at javafxapp.FXMLDocumentController.sendMsg(FXMLDocumentController.java:40)
 ... 73 more

我不知道是什么导致了错误,但是 'handleButtonAction' 方法工作正常。 "apologize if my presentation of question is wrong, I've never asked anything here before"

您的元素可能没有正确连接到 FXML。检查您的 FXML 中的 chatArea 和 chatText 是否设置为 fx:id。

好的。因此,我知道有两种 不同的 方法可以解决此问题。

技巧 1) 要按照您已经在做的方式,试试这个:

@FXML
    private void handleButtonAction(javafx.event.ActionEvent event) throws IOException
{//etc...}

技巧 2) 您也可以创建一个普通方法,然后从场景生成器中调用它,如下所示:

public void handleButtonAction()
{
      //your code goes here
}

现在,进入场景生成器,单击按钮,导航到侧面板的 "code" 部分,然后查找 "On Action"。在 "On Action" 中,输入您的方法名称(不带括号)。因此,在这种情况下,您只需键入 "handleButtonAction"。保存文档。

现在应该可以了。

第二种是我在这种情况下推荐的技术。它更干净,更美观。

如果有帮助请告诉我:)