e4 应用程序/菜单
e4 Application / Menu
我尝试创建一个 eclipse 4 应用程序,但我遇到了一个问题:"Menu/New":当我尝试创建一个新文件时,我收到此错误消息:
Internal error org.eclipse.E4.core.di.InjectionException:
java.lang.NullPointerException
这是代码:
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.");
}
}
}
}
有人可以帮帮我吗???提前致谢
因为你没有向我们展示我只是猜测的堆栈跟踪,但是行
((EditorPart) part.getObject()).styledText.setText(text);
可能在 styledText
字段创建之前访问它。
创建部件控件时,您需要在 EditorPart
的 @PostConstruct
方法中执行此操作。
我尝试创建一个 eclipse 4 应用程序,但我遇到了一个问题:"Menu/New":当我尝试创建一个新文件时,我收到此错误消息:
Internal error org.eclipse.E4.core.di.InjectionException: java.lang.NullPointerException
这是代码:
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.");
}
}
}
}
有人可以帮帮我吗???提前致谢
因为你没有向我们展示我只是猜测的堆栈跟踪,但是行
((EditorPart) part.getObject()).styledText.setText(text);
可能在 styledText
字段创建之前访问它。
创建部件控件时,您需要在 EditorPart
的 @PostConstruct
方法中执行此操作。