简体   繁体   中英

e4 Application / Menu

i try to create an eclipse 4 application but i have a problem: "Menu/New" : when i try to create a new file i receive this error message :

Internal error org.eclipse.E4.core.di.InjectionException: java.lang.NullPointerException

this's the code:

public class OpenHandler {
@Execute
public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell, EPartService partService, MApplication application,
                EModelService modelService) {
    FileDialog dialog = new FileDialog(shell);
    String fileName = dialog.open();
    if (fileName != null) {
        try {
            File file = new File(fileName);
            Scanner scanner = new Scanner(file);
        String text = scanner.useDelimiter("\\A").next();
            scanner.close();
            // create part
            MPart part = MBasicFactory.INSTANCE.createPart();
            part.setLabel(file.getName());
            part.setCloseable(true);
            part.getTags().add(EPartService.REMOVE_ON_HIDE_TAG);
            part.setContributionURI("bundleclass://RCPTextEditor/RCPTextEditor.EditorPart");
            // get part stack and show new part 
            List<MPartStack> stacks = modelService.findElements(application, null, MPartStack.class, null);
            stacks.get(0).getChildren().add(part);
            partService.showPart(part, PartState.ACTIVATE);
            ((EditorPart) part.getObject()).styledText.setText(text);
            ((EditorPart) part.getObject()).model = new EditorModel(text, text);
            ((EditorPart) part.getObject()).setFile(file);
            part.setDirty(false);
        } catch (IOException e) {
            MessageDialog.openError(shell, "Error opening file", "File " + fileName + " could not be opened.");
        }
    }
}
}

Can someone help me ??? thanks in advance

Since you haven't show us the stack trace I am just guessing but the line

 ((EditorPart) part.getObject()).styledText.setText(text);

is probably accessing a styledText field before it has been created.

You need to do this in the @PostConstruct method of your EditorPart when you create the part controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM