【问题标题】:NullPointerException Error when trying to set javafx TextField text from JnativeHook mouse events尝试从 JnativeHook 鼠标事件设置 javafx TextField 文本时出现 NullPointerException 错误
【发布时间】:2015-08-21 00:14:43
【问题描述】:

我正在尝试在 JnativeHook MoseClicked 事件发生时设置 javafx TextField 文本。但我遇到了“NullPointerException”错误。我将控制器类代码放在这里:

public class FXMLDocumentController implements Initializable, NativeMouseListener {

@FXML
private TextField txt_Search;

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

   txt_Search.setText("dvdf"); //this is work and no problem is in here
           Listener();
}
public void Listener() {
    // Clear previous logging configurations.
    LogManager.getLogManager().reset();
    // Get the logger for "org.jnativehook" and set the level to off.
    Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
    logger.setLevel(Level.OFF);
    try {
        GlobalScreen.registerNativeHook();
    } catch (NativeHookException ex) {
        System.err.println("There was a problem registering the native hook.");
        System.err.println(ex.getMessage());

        System.exit(1);
    }
    // Construct the example object.
    FXMLDocumentController example = new FXMLDocumentController();
    // Add the appropriate listeners.
    GlobalScreen.addNativeMouseListener(example);
}

 @Override
public void nativeMouseClicked(NativeMouseEvent nme) {
    if (nme.getClickCount() == 2) {
        System.out.println("Double Click Event");
        txt_Search.setText("Mouse clicked");
    }

}

@Override
public void nativeMousePressed(NativeMouseEvent nme) {
    // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void nativeMouseReleased(NativeMouseEvent nme) {
    //   throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}

这个错误正在发生:

双击事件
线程“JNativeHook 调度线程”中的异常 java.lang.NullPointerException
在 FXMLDocumentController.nativeMouseClicked(FXMLDocumentController.java:60)
在 org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(未知来源)
在 org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(未知来源)
在 org.jnativehook.GlobalScreen$EventDispatchTask.run(未知来源)
在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
在 java.lang.Thread.run(Thread.java:745)

更新:

我更新了我的代码并使用 @FXML 注释对 txt_Search 进行了注释

【问题讨论】:

  • 当您尝试使用 txt_Search 时,它似乎为空。
  • 我不想读取字段值。而当我想在初始化方法中将文本设置为 txt_Search 时没有问题。只有在初始化方法之外才会发生此错误。

标签: java javafx nullpointerexception jnativehook


【解决方案1】:

您应该返回并查看使用 wiki 的 working with swing 部分。默认情况下,此库生成的事件在 Swing 事件调度线程上运行!您必须在注册钩子之前包装对摆动组件的访问或使用GlobalScreen.setEventDispatcher(new SwingDispatchService());。更多信息,请阅读event dispatch threadSwing thread safety

【讨论】:

  • 感谢您的帮助。是 javafx 的类似解决方案。我不想使用 Swing 组件
  • 能否告诉我 JavaFX 的解决方案是什么,以便我可以在 Wiki 中添加注释?
【解决方案2】:

@Kingtak 你从来没有初始化你的 'txt_Search' 变量。你可以在这里使用@FXML 注释,并在 fxml 文件中将 id 分配给那里的文本字段。

【讨论】:

  • txt_Search 是 textField 的 fx:id。我添加了@FXML 注解。但是问题没有解决
  • 我认为 txt_Search 字段中没有问题。问题就在初始化方法之外。
猜你喜欢
  • 2016-04-29
  • 2010-12-11
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多