0

I have a keyboard emulation class which has a method to type to the Eclipse editor automatically based on voice recognition input. This works correctly.

I also have tables made in JavaFX, in which a user clicks an element and it too is emulated to the screen using the same method.

However, I am getting "Exception in thread "JavaFX Application Thread" java.lang.NullPointerException" exceptions. This does not happen from the voice recognition part, only when the JavaFX thread calls the method to update the Eclipse editor.

Keyboard Emulation method which prints data to the eclipse text editor

 private void doType(int[] keyCodes, int offset, int length) {
    if (length == 0) {
        return;
    }

    //Since we are in a loop, we must only emulate the mouse press on the eclipse
    //text editors last known location to avoid every letter getting mixed up
    correctCaretPosition++;


        Display.getDefault().syncExec(new Runnable() {
        public void run() { 

            System.out.println("fwefwefwef\n\n\n\n\n\n" + myEditor.PROP_DIRTY);

            Control control = myEditor.getAdapter(Control.class);
            //Only when the click to bring eclipse into scope has not happened
            if (control instanceof StyledText && correctCaretPosition <= 1) {
                  StyledText text = (StyledText)control;

                  // Position of caret relative to top left of the control
                  Point relPos = text.getLocationAtOffset(text.getCaretOffset()); 

                  // Position relative to display
                  Point absPos = text.toDisplay(relPos); 
                  System.out.println(absPos.x + "  " +absPos.y);
                  robot.mouseMove(absPos.x, absPos.y);
                  robot.mousePress(BUTTON1_MASK);
                  robot.mouseRelease(BUTTON1_MASK);
                }

        robot.keyPress(keyCodes[offset]);
        doType(keyCodes, offset + 1, length - 1);
        robot.keyRelease(keyCodes[offset]);
              }
        });

    }

JavaFX call to Keyboard Emulation

 Platform.runLater(new Runnable() {
         @Override
         public void run()
         {
                robot.type(data);       
        }
      });

The error comes from this line

Control control = myEditor.getAdapter(Control.class);

Robot in this context is the Keybooard Emulation object. Am I getting the concurrency between the two completely wrong?

UPDATE:

The root of the error is

Control control = myEditor.getAdapter(Control.class);

I passed in the control from the beginning of the application, no error, however, nothing happens as I believe the control does not satify

if (control instanceof StyledText && correctCaretPosition <= 1) {

Updated Stack-trace

Exception in thread "JavaFX Application Thread" 
!ENTRY org.eclipse.ui 4 0 2016-02-24 16:36:49.413
!MESSAGE Unhandled event loop exception
org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.NullPointerException)
at org.eclipse.swt.SWT.error(SWT.java:4491)
at org.eclipse.swt.SWT.error(SWT.java:4406)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:208)
at org.eclipse.ui.internal.UISynchronizer.syncExec(UISynchronizer.java:145)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4761)
at voice.recognition.Keyboard_Emulation.doType(Keyboard_Emulation.java:204)
at voice.recognition.Keyboard_Emulation.doType(Keyboard_Emulation.java:191)
at voice.recognition.Keyboard_Emulation.type(Keyboard_Emulation.java:174)
at voice.recognition.Keyboard_Emulation.type(Keyboard_Emulation.java:69)
at table.symbol.SymbolTableController$1$1.run(SymbolTableController.java:92)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at voice.recognition.Keyboard_Emulation$1.run(Keyboard_Emulation.java:208)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4155)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3772)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
!STACK 0
org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.NullPointerException)
at org.eclipse.swt.SWT.error(SWT.java:4491)
at org.eclipse.swt.SWT.error(SWT.java:4406)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:138)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4155)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3772)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$4.run(PartRenderingEngine.java:1127)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1018)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:156)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:654)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
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 org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
Caused by: java.lang.NullPointerException
at voice.recognition.Keyboard_Emulation$1.run(Keyboard_Emulation.java:208)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
... 24 more
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:337)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:598)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:139)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
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 org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:669)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:608)
at org.eclipse.equinox.launcher.Main.run(Main.java:1515)
at org.eclipse.equinox.launcher.Main.main(Main.java:1488)
  • Is it possible to see the stack trace for the error? It should highlight the area of concern. It's hard to tell here whether the null reference exception is coming from robot, data or control. Or somewhere within the getAdapter() call. The runLater call may be causing an issue also, if something hasn't been instantiated at the point of it being called. – ManoDestra Feb 24 '16 at 16:24
  • Everything is working fine when JavaFX is not involved so I wouldn't think its a null reference per say... – UnholySalmon Feb 24 '16 at 16:25
  • Well, it's certainly a null pointer exception that's being bubbled up, although that may be deceiving. Seems to be originating from the `table.symbol.SymbolTableController$1.handle()` method. I'd get some debugging statements into the code to check for the existence of nulls for various values of concern. It could also be a concurrency issue across the various threads employed by JavaFX, but I'd start here and it should give you some idea of where to look next. – ManoDestra Feb 24 '16 at 16:31
  • Could it be that the call to the emulation is coming from a JavaFX thread, this then tries to get the getAdapter(Control.class); which is of SWT widget? – UnholySalmon Feb 24 '16 at 16:40
  • Yes, that's certainly possible, if you're intermingling use of SWT and JavaFX. – ManoDestra Feb 24 '16 at 16:43
  • Take a look here, worth checking out: http://stackoverflow.com/questions/16708931/javafx-working-with-threads-and-gui – ManoDestra Feb 24 '16 at 16:44
  • I think the concurrency is probably not the cause here. What is on line 87 of `SymbolTableController`? – James_D Feb 24 '16 at 16:46
  • 87 Platform.runLater(new Runnable() { @Override public void run() { robot.type(data); } }); – UnholySalmon Feb 24 '16 at 16:53
  • The stack trace looks more like a `Display.asyncExec` runnable is running rather than `syncExec`. – greg-449 Feb 24 '16 at 17:24
  • It is definable "Control control = myEditor.getAdapter(Control.class);" causing the error...... I have an instance of the editor passed in but the getAdapter(Contol.class) returns null.... Any idea why? – UnholySalmon Feb 24 '16 at 17:27

0 Answers0