Skip to content

NullPointerException if "module" statement is removed (or entire contents) after setting selection #55

@davidmichaelkarr

Description

@davidmichaelkarr

It's an odd test case, but I usually can repeat this sequence:

  1. While viewing a valid Yang file, select some text in the buffer.
  2. Select entire buffer.
  3. Press Delete to empty buffer.

This will often result in the following stacktrace:

java.lang.NullPointerException
    at com.cisco.yangide.editor.editors.YangEditor$YangEditorSelectionChangedListener.selectionChanged(YangEditor.java:154)
    at org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2751)
    at org.eclipse.jface.text.TextViewer.firePostSelectionChanged(TextViewer.java:2699)
    at org.eclipse.jface.text.TextViewer$5.run(TextViewer.java:2678)
    at org.eclipse.swt.widgets.Display.timerProc(Display.java:4406)
    at org.eclipse.swt.internal.gtk.OS._g_main_context_iteration(Native Method)
    at org.eclipse.swt.internal.gtk.OS.g_main_context_iteration(OS.java:2425)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3428)
    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(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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)

Here is the method in question at the top of stack:

    private class YangEditorSelectionChangedListener extends AbstractSelectionChangedListener {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            if (event.getSelection() instanceof ITextSelection) {
                ITextSelection textSelection = (ITextSelection) event.getSelection();
                try {
                    if (null != outlinePage) { // next line is line 154
                        outlinePage.selectNode(getModule().getNodeAtPosition(textSelection.getOffset()));
                    }
                } catch (YangModelException e) {
                }
            }

        }
    }

From debugging, I found that "getModule()" returns null in this situation, although there also appears to be a race condition in play that I don't clearly understand. I tried adding an "if" specifically for "getModule() == null" before the "if" on line 153, with a print statement saying that "getModule()" returned null. I set a breakpoint there, and when I hit that, I executed "getModule()" in the display view, and it gave me a non-null value.

It seems safe to amend the "if" condition on line 153, adding "null != getModule()". A brief comment about this situation before the "if" would be beneficial also.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions