1

I am trying to set javafx TextField text when JnativeHook MoseClicked event happens.But i face with "NullPointerException" error.I put my Controller class code in here:

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.
}

}

And this error is occurring :

Double Click Event
Exception in thread "JNativeHook Dispatch Thread" java.lang.NullPointerException
at FXMLDocumentController.nativeMouseClicked(FXMLDocumentController.java:60)
at org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source)
at org.jnativehook.GlobalScreen$EventDispatchTask.processButtonEvent(Unknown Source)
at org.jnativehook.GlobalScreen$EventDispatchTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Update:

I update my code and annotate txt_Search with @FXML annotation

Leif Gruenwoldt
  • 13,561
  • 5
  • 60
  • 64
Kingtak
  • 125
  • 3
  • 6
  • Looks like `txt_Search` is null when you try to use it. – Lucas Ross Jun 06 '15 at 01:09
  • I don't want to read field value. While when I want to set text to txt_Search in the initialize method do not problem.Only in outside of initialize method this error is occurring. – Kingtak Jun 06 '15 at 06:55

2 Answers2

0

@Kingtak you never initialized your 'txt_Search' variable.You can use @FXML annotation here and in fxml file assign the id to the textfield there.

Aman
  • 23
  • 4
  • the txt_Search is fx:id of textField.I add @FXML annotation.But the problem is not solved – Kingtak Jun 06 '15 at 07:13
  • I do not think the problem in the field txt_Search . The problem is just outside the initialize method. – Kingtak Jun 06 '15 at 07:27
0

You should go back and review the working with swing section of the usage wiki. The events produced by this library do not operate on the Swing event dispatch thread by default! You must wrap access to swing components OR use GlobalScreen.setEventDispatcher(new SwingDispatchService()); before registering the hook. For more information, please read about the event dispatch thread and Swing thread safety.

Alex Barker
  • 4,316
  • 4
  • 28
  • 47