6

I wonder about a Java error that is repeatedly occurs in MATLAB. It typically occurs when MATLAB is doing some heavy stuff with Java. This can for example be holding Ctrl + Z or Ctrl + Y.

I did by mistake erase the error message before I copied it, but I think that I can pass the core of the problem anyway.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
...
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Why did this error occur? I have found some information about this from MATLAB r2007, this due to that Java Swing is thread unsafe and MATLAB lacked support to ensure thread safety. However, that is supposed to have been fixed in MATLAB r2008b. So why do I get it now?

Here is the full stack trace:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.netbeans.editor.BaseDocument.notifyUnmodify(BaseDocument.java:1465)
at org.netbeans.editor.BaseDocument.notifyModifyCheckEnd(BaseDocument.java:816)
at org.netbeans.editor.BaseDocumentEvent.redo(BaseDocumentEvent.java:336)
at javax.swing.undo.UndoManager.redoTo(Unknown Source)
at javax.swing.undo.UndoManager.redo(Unknown Source)
at com.mathworks.mwswing.undo.MUndoManager.redo(MUndoManager.java:255)
at org.netbeans.editor.ActionFactory$RedoAction.actionPerformed(ActionFactory.java:767)
at org.netbeans.editor.BaseAction.actionPerformed(BaseAction.java:259)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at com.mathworks.widgets.SyntaxTextPaneBase.processKeyEvent(SyntaxTextPaneBase.java:1187)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
patrik
  • 4,506
  • 6
  • 24
  • 48
  • 1
    The exception is telling you that some object is `null` when you call some method on it, but without the complete stack trace nobody can tell you which object is that. Please include full stack trace. You can find a list of tipical causes here: [nullpointerexception](http://stackoverflow.com/tags/nullpointerexception/info) – dic19 Jul 29 '14 at 13:06
  • 2
    @dic19 I am afraid this is impossible, the error is not really reproducable. It occurs at some occasions only, but can still be really annoying. The code belongs to the core of matlab and the source cose is not available. I have actually got the error for different objects. Mouse events, matlab gui... I think the problem is caused in an earlier stage. Probably due to some corrupt object or some thread starting before it is supposed to. However, I understand if you have problems when you cannot see the full stack. – patrik Jul 29 '14 at 13:12
  • 2
    Well in that case there's no much we can do about it, is it? Threading issues makes sense to me, considering the [Event Dispatch Thread](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) is prone to cause troubles. I'd start looking at matlabs forums if there is some known bug causing random behavior as you describe. – dic19 Jul 29 '14 at 13:30
  • @dic19 Ok I managed to force the error again. See edit. However, the link from The [previous comment](http://stackoverflow.com/questions/25015983/why-do-i-get-the-exception-exception-in-thread-awt-eventqueue-0-java-lang-nul#comment38901439_25015983) may have given the answer. The error occurs in most cases when matlab needs to do heavy work, eg hold ctrl+y (redo) after a long ctrl+z (undo) for long pieces of code. These task may be to long for EDT when pumped rapidly. I am not sure though. And about scanning matlab forums: there are a lot of questions about this, but preciously few answers. – patrik Jul 29 '14 at 13:40
  • Please see my answer. It's not a *definitive* answer but a reasonable explanation to your problem (I think). Unfortunately there seems to be no solution to this problem, at least as far as I can see. Good luck anyway! :) – dic19 Jul 29 '14 at 15:04
  • @EJP this isn't a dupe of that question - the NPE is coming from the Matlab application, not OP's code. That might mean the question isn't on-topic for SO, but the duped question doesn't answer this one. – dimo414 Nov 27 '17 at 06:51
  • @dimo414 After a fairly long time I have realized that this error might be related to the performance of the device. There Error seems to occur when there are more events created than the event handler can process and the origin of this exception is still obscure. I have also been in contact with Mathworks and they have never been able to reproduce this error. – patrik Nov 27 '17 at 14:47

2 Answers2

2

Well, based on your stack trace, probably there isn’t any definitive answer to your question, as you have already seen in MATLAB's forum, but given this line, I think there's a possible explanation:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    ...
    at javax.swing.undo.UndoManager.redoTo(Unknown Source) // <-- here!
    at javax.swing.undo.UndoManager.redo(Unknown Source)
    at com.mathworks.mwswing.undo.MUndoManager.redo(MUndoManager.java:255)
    ...

UndoManager class keeps an internal collection of UndoableEdit objects. This collection is actually inherithed from its superclass: CompoundEdit.

The internal implementation of UndoManager#redo() and UndoManager#redoTo(UndoableEdit edit) looks like this:

public class UndoManager extends CompoundEdit implements UndoableEditListener {
    ...
    public synchronized void redo() throws CannotRedoException {
        if (inProgress) {
            UndoableEdit edit = editToBeRedone();
            if (edit == null) {
                throw new CannotRedoException();
            }
            redoTo(edit);
        } else {
            super.redo();
        }
    }
    ...
    protected void redoTo(UndoableEdit edit) throws CannotRedoException {
        boolean done = false;
        while (!done) {
            UndoableEdit next = edits.elementAt(indexOfNextAdd++);
            next.redo(); // NPE here?
            done = next == edit;
        }
    }
    ...
}

Considering this implementation and given that Swing's Event Dispatch Thread (EDT) is prone to cause troubles, I think it's probably a threading issue between MATLAB threads and the EDT. Specifically speaking this MATLAB-invoked method could be the source of the problem:

at com.mathworks.mwswing.undo.MUndoManager.redo(MUndoManager.java:255)

Since you say MATLAB needs to do heavy work, it's not unreasonable to think this method is trying to redo some edit that might well not be available anymore or might not be available yet, due to synchronization problems with the EDT.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    This seems as it could be the root. Thank you for the explanation! I sent a bug report an guess I must hope for the best for the next release. – patrik Jul 29 '14 at 16:15
-3

You can find the ~/.matlab folder which contains MATLAB settings, etc. Use ls -la to show all hidden files and folders.

Open a terminal and execute sudo chmod 757 -R ~/.matlab.

Similarly there is a folder, MATLAB, in Documents. Execute sudo chmod 757 -R ~/Documents/MATLAB.

Now restart MATLAB without root privileges. It worked for me on Ubuntu 14.04 (Trusty Tahr) and MATLAB 2015a.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    I am not sure this is the problem. The issue appears only while spawning a lot of events, so it is likely a threading issue. Apart from that I use Windows 7. Maybe I should have written that as well, but I did not see the need back then. This is a java problem so I though it to be fairly system independent. I realize now that this may not be entirely correct, but in that case it should rather be due to different thread handling. – patrik Apr 05 '16 at 06:28