Mysterious java error after my MATLAB program finishes

Hi all,
My huge psychophysics program runs fine, with no error, but, after it ends, a long red error message spews forth into the Command Window (below). There seems to be no ill effect in ignoring it, but I’d at least like to have some idea what MATLAB is complaining about. I find it utterly opaque. Note that I connect a wireless keyboard while running my program, and that bit of code calls “clear KbWait”. I’ve pasted a table at the bottom giving specs of my set up (from IdentifyComputer). Any ideas?
Best
Denis

Exception in thread “AWT-EventQueue-0”: java.lang.NullPointerException
at sun.lwawt.macosx.CAccessibility.propertyChange(CAccessibility.java:83)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.KeyboardFocusManager.firePropertyChange(KeyboardFocusManager.java:1493)
at java.awt.KeyboardFocusManager.setGlobalFocusOwner(KeyboardFocusManager.java:575)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:603)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Exception in thread “AWT-EventQueue-0”: java.lang.NullPointerException
at sun.lwawt.macosx.CAccessibility.propertyChange(CAccessibility.java:83)
at java.beans.PropertyChangeSupport.fire(PropertyChangeSupport.java:335)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:328)
at java.beans.PropertyChangeSupport.firePropertyChange(PropertyChangeSupport.java:263)
at java.awt.KeyboardFocusManager.firePropertyChange(KeyboardFocusManager.java:1493)
at java.awt.KeyboardFocusManager.setGlobalFocusOwner(KeyboardFocusManager.java:575)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:603)
at java.awt.Component.dispatchEventImpl(Component.java:4760)
at java.awt.Container.dispatchEventImpl(Container.java:2297)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
at java.awt.EventQueue$4.run(EventQueue.java:733)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

                   model: 'MacBookPro14,3'
        modelDescription: 'MacBook Pro (15-inch, 2017)'
            manufacturer: 'Apple Inc.'
            psychtoolbox: 'Psychtoolbox 3.0.16'
                  matlab: 'MATLAB 9.6 (R2019a)'
                  system: 'macOS 10.14.6'
               screenMex: 'Screen.mexmaci64 Apr 26 2020'
                 screens: 0
                  screen: {[0]}
                    size: {[2880 1800]}
              nativeSize: {[2880 1800]}
                      mm: {[331 206]}
          openGLRenderer: {'AMD Radeon Pro 560 OpenGL Engine'}
            openGLVendor: {'ATI Technologies Inc.'}
           openGLVersion: {'2.1 ATI-2.11.21'}
psychtoolboxKernelDriver: 'PsychtoolboxKernelDriver 1.1'
          drawTextPlugin: 1
          psychPortAudio: 1
                 summary: 'MacBookPro14,3-macOS-10.14.6-PTB-3.0.16'

No, i have no idea. If this is repeatable, try to trim your code down to the essentials that still cause this. Perhaps that gives a clue then for us (or MATLAB support)

As i told you repeatedly, “clear KbWait” or “clear SomePTBFunction” or such is usually a bad idea. “clear all” is the only sane way to restore everything to sane defaults. But this is unrelated to what you reported.

It is some Java error in Matlab’s Java-based GUI. Could be Matlab’s fault, Java’s fault, or a macOS bug. Never saw this one. Googling the error message suggests rebooting the machine may help, which would be consistent with macOS bugs.

Dear Mario
thanks for the attempted diagnosis and helpful comments.

However, your recommendation of “clear all” instead of clearing particular mex files, and your passing comment about interdependence of the mex files, seems out of synch with the Mathworks advice of avoiding “clear all”. Some of the drivers used by Psychtoolbox (.e.g PsychHID and ) are easily confused (e.g. when we open them and quit without closing them), and then stay confused until cleared. Having my psychophysical testing software fail is a big problem for my lab. The observers are alarmed by errors and don’t know what to do, so they send the experimenter a text message, but often the reply is too late, and we’ve lost the session. Many of my programs had calls to clear troublesome drivers to try to make sure it was in a good state before we use it. I’m gradually removing those calls, but “clear all” is not a good substitute.

In particular, when my program tells the user to connect a wireless keyboard because the tests need long viewing distance, we need to do a new enumeration of keyboards. I wrote this code long ago, perhaps with your help. Here’s the key part:

% Force new enumeration of devices to detect newly attached external
% keyboard.
clear PsychHID;
% Clear cache of keyboard devices.
clear KbCheck;
Is it possible to add a keyboard without clearing PsychHID and KbCheck?

A lot has been set up at the point that we make these calls. Calling “clear all” is not an option.

Your comments suggest that it’s obvious that we should never try to clear particular MEX files. On the contrary, I don’t understand why the psychtoolbox MEX files are interdependent. Shouldn’t independence be the goal? Would it be enough for each Psychtoolbox MEX file, when it’s starting, to alert the other relevant Psychtoolbox MEXs, perhaps by setting a global flag?

Thanks.
Best
Denis