将AbstractDecoratedTextEditor添加到Eclipse中的Multipage编辑器之后的NPE

时间:2015-09-08 21:58:45

标签: java eclipse eclipse-rcp text-editor

我正在创建一个简单的Eclipse编辑器,这个编辑器包含两个页面,其中一个是从AbstractDecoratedTextEditor扩展的编辑器,然后在我的多页编辑器类I中添加编辑器这样:

MyEditor myEditor = new MyEditor();
addPage(myEditor, myEditor.getEditorInput());

当我启动应用程序时,编辑器看起来像这样: enter image description here

这是我的班级:

public class MyEditor extends AbstractDecoratedTextEditor {

    public MyEditor() {
        setSourceViewerConfiguration(new SourceViewerConfiguration());
        setDocumentProvider(new FileDocumentProvider());
    }

    @Override
    protected boolean isLineNumberRulerVisible() {
        return true;
    } 

    public void init(IEditorSite site, IEditorInput input) {
       setSite(site);
       setInput(input);
    }
}

这是日志:

java.lang.NullPointerException
    at org.eclipse.ui.texteditor.AddMarkerAction.getResource(AddMarkerAction.java:336)
    at org.eclipse.ui.texteditor.AddMarkerAction.update(AddMarkerAction.java:162)
    at org.eclipse.ui.texteditor.TextEditorAction.<init>(TextEditorAction.java:49)
    at org.eclipse.ui.texteditor.AddMarkerAction.<init>(AddMarkerAction.java:96)
    at org.eclipse.ui.texteditor.AbstractDecoratedTextEditor.createActions(AbstractDecoratedTextEditor.java:1153)
    at org.eclipse.ui.texteditor.AbstractTextEditor.createPartControl(AbstractTextEditor.java:3576)
    at org.eclipse.ui.texteditor.StatusTextEditor.createPartControl(StatusTextEditor.java:54)
    at org.eclipse.ui.texteditor.AbstractDecoratedTextEditor.createPartControl(AbstractDecoratedTextEditor.java:447)
    at org.eclipse.ui.part.MultiPageEditorPart.addPage(MultiPageEditorPart.java:243)
    at org.eclipse.ui.forms.editor.FormEditor.addPage(FormEditor.java:325)
    ....

1 个答案:

答案 0 :(得分:1)

您的MyEditor没有设置编辑器输入。

在您的代码中:

MyEditor myEditor = new MyEditor();   
addPage(myEditor, myEditor.getEditorInput());

myEditor.getEditorInput()将返回null,因为您尚未为编辑器设置输入。

您必须将正确的编辑器输入传递给addPage调用(通常是多页编辑器的输入)。然后,这将在编辑器中设置输入。