Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Eclipse插件中将文件打开到某一行_Java_Eclipse_Plugins_Eclipse Plugin - Fatal编程技术网

Java 在Eclipse插件中将文件打开到某一行

Java 在Eclipse插件中将文件打开到某一行,java,eclipse,plugins,eclipse-plugin,Java,Eclipse,Plugins,Eclipse Plugin,我正在编写一个插件,当按下按钮时,它必须在某一行打开一个文件。 我有以下代码在某一行打开文件 String filePath = "file path" ; final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath)); if (inputFile != null) {

我正在编写一个插件,当按下按钮时,它必须在某一行打开一个文件。 我有以下代码在某一行打开文件

        String filePath = "file path" ;
            final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
            if (inputFile != null) {
                IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                IEditorPart openEditor11 = IDE.openEditor(page1, inputFile);
            }


            int Line = 20;

                    if (openEditor11 instanceof ITextEditor) {
                        ITextEditor textEditor = (ITextEditor) openEditor ;
                        IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
                        textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
                    }

我的问题是if语句中的变量openEditor11给出了错误:openEditor11无法解析为变量。有什么问题吗?

这是因为嵌套在if语句中的变量声明在完成条件时超出了范围。因此,变量被释放,并且在第二条语句中不再存在

您应该事先声明它以避免此错误,如下所示:

IEditorPart openEditor11;
String filePath = "file path" ;
final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
if (inputFile != null) {
    IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    openEditor11 = IDE.openEditor(page1, inputFile);
}


int Line = 20;

if (openEditor11 instanceof ITextEditor) {
    ITextEditor textEditor = (ITextEditor) openEditor ;
    IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
}

到达第二个条件块时,如果第一个条件不适用,编辑器可能为null,但这不是问题,因为
instanceof
在null时返回false。

这是因为在完成条件时嵌套在if语句中的变量声明超出范围。因此,变量被释放,并且在第二条语句中不再存在

您应该事先声明它以避免此错误,如下所示:

IEditorPart openEditor11;
String filePath = "file path" ;
final IFile inputFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(Path.fromOSString(filePath));
if (inputFile != null) {
    IWorkbenchPage page1 = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    openEditor11 = IDE.openEditor(page1, inputFile);
}


int Line = 20;

if (openEditor11 instanceof ITextEditor) {
    ITextEditor textEditor = (ITextEditor) openEditor ;
    IDocument document= textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    textEditor.selectAndReveal(document.getLineOffset(Line - 1), document.getLineLength(Line-1));
}

到达第二个条件块时,如果第一个条件不适用,编辑器可能为null,但这不是问题,因为
instanceof
在null上返回false。

根据


实际上,有一个更简单的解决方案


问题在于openEditor11无法解析为变量。:)如果你重温一门Java课程,你会帮自己一个忙,因为在掌握基础知识之前潜入任何编程都是一件痛苦的事。我现在在一个项目中尝试学习Java。我已经做了一个名为eclipselink的项目。几年前在eclipseplugincentral.com上发布。现在这里供应:谢谢!我只尝试学习一些Java和Eclipse插件:)我有问题决定如果文件被打开两次,我应该关注哪个编辑器;如果Eclipse窗口被克隆,我应该使用哪个窗口;如果用户启动多个Eclipse实例,Eclipse应该接受哪个请求(最后一个问题可能不是你的,因为你知道你的Eclipse)。问题在于openEditor11无法解析为变量。:)如果你重温一门Java课程,你会帮自己一个忙,因为在掌握基础知识之前潜入任何编程都是一件痛苦的事。我现在在一个项目中尝试学习Java。我已经做了一个名为eclipselink的项目。几年前在eclipseplugincentral.com上发布。现在这里供应:谢谢!我只想学习一些Java和Eclipse插件:)我很难决定如果文件被打开两次,我应该关注哪个编辑器;如果Eclipse窗口被克隆,我应该使用哪个窗口;如果用户启动多个Eclipse实例,Eclipse应该接受哪个请求(最后一个问题可能不是你的,因为你知道你的Eclipse)。