Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - Can Anyone tell me why my IFile always returns null?

I have a plugin. Within this plugin I have a view that creates some markers so that I can open the file and navigate automatically to a selected line. However where as this method has worked for me previously. it ceases to now? it only ever returns null as my IFile.

Here is my method of creating the markers NOTE: That this method is not located within the controlling class of the FXML file. it is located in another external file.

public static String openAbsoluteFileInEclipseEditor(String absoluteLocationP, int lineNumberP) {

    File absolute = new File(absoluteLocationP);
    if(absolute.isFile() && absolute.exists()) {  
        if(Globals.testing) {
            try {
                Desktop.getDesktop().open(absolute);
                return null;
            } catch (IOException e) {
                ErrorHandling.reportErrors(e);
                return "";
            }
        }else {
            IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
            IWorkbenchPage page = window.getActivePage();
            IWorkspace workspace = ResourcesPlugin.getWorkspace(); 

            try {  
                if(lineNumberP != 0) {  

                    IPath location = Path.fromOSString(absolute.getAbsolutePath()); 
                    System.out.println("location " + location);
                    
                    IFile iFile = workspace.getRoot().getFileForLocation(location); 
                    System.out.println("iFile " + iFile);
                    
                    IMarker marker = iFile.createMarker(IMarker.TEXT);
                    marker.setAttribute(IMarker.LINE_NUMBER, lineNumberP);
                    IDE.openEditor(page, marker);
                    marker.delete();
                }else {
                    IFileStore fileStore = EFS.getLocalFileSystem().getStore(absolute.toURI());
                    IDE.openEditorOnFileStore( page, fileStore );
                }
                return null;
            } catch (PartInitException e) {
                ErrorHandling.reportErrors(e);
                return "";
            } catch (CoreException e) {
                ErrorHandling.reportErrors(e);
                return "";
            }
        }
    }else {
        return "File not found";
    }
}

Here are the two prints values that you can see in the middle of the method.

location C:/SoftwareAG_Workspaces/workspace105/HOSPITAL/Natural-Libraries/HOSPITAL/Programs/XX021P01.NSP iFile null

Can anyone point out to me why it might no longer work and why its only returning nulls? and if possible could you suggest an alternate method that will work? the file does exist within in that location I have made sure of that.

Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The Javadoc for getFileForLocation says:

This method returns null when the given file system location is not under the location of any existing project in the workspace.

So is that location in the current workspace, and in a valid project?

The Javadoc also says:

The result will also omit resources that are explicitly excluded from the workspace according to existing resource filters.

So check any resource filters.

Finally the Javadoc says:

Warning: This method ignores linked resources and their children. Since linked resources may overlap other resources, a unique mapping from a file system location to a single resource is not guaranteed. To find all resources for a given location, including linked resources, use the method findFilesForLocation.

So check for linked resources


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...