0

The problem is that the command object in my code is null and i don't know why.

I use the e4-perspective-switcher from Lars Vogel together with the question from here How to add Perspective Bar Switcher to pure eclipse 4 rcp application.

public class PerspectiveSwitcher {

    @Inject
    private ECommandService commandService;

    @Inject
    private EHandlerService handlerService;


    @PostConstruct
    public void createControls(Composite parent, EModelService modelService) {
        Composite body = new Composite(parent, SWT.NONE);
        body.setLayout(new GridLayout());
        body.setBackground(parent.getBackground());

        Button button = new Button(body, SWT.PUSH);
        button.setText("switch");
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, false);
        data.minimumHeight = 20;
        button.setLayoutData(data);
        button.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                List<MPerspective> perspectives = modelService.findElements(app,
                        PTApplicationConstants.PERSPECTIVE_ID_MANAGER, MPerspective.class, null);
                MPerspective perspective = perspectives.get(0);

                HashMap<String, Object> parameters = new HashMap<>(2);
                parameters.put(E4WorkbenchParameterConstants.COMMAND_PERSPECTIVE_ID, perspective.getElementId());
                parameters.put(E4WorkbenchParameterConstants.COMMAND_PERSPECTIVE_NEW_WINDOW, "false");

                ParameterizedCommand command = commandService
                        .createCommand(E4WorkbenchCommandConstants.PERSPECTIVES_SHOW_PERSPECTIVE, parameters);
                handlerService.executeHandler(command);

                partService.switchPerspective(perspective);

                super.widgetSelected(e);
            }
        });
    }
}
greg-449
  • 109,219
  • 232
  • 102
  • 145
Darksmilie
  • 193
  • 1
  • 11
  • Are you talking about the `command` variable? Have you declared the command in the Application.e4xmi (or fragment.e4xmi)? – greg-449 Mar 25 '19 at 08:25
  • i create a command with the id from the "createCommand" method in the Application.e4xmi but the command variable is still null. `````` – Darksmilie Mar 25 '19 at 09:31
  • Did you specify `-clearPersistedState` when running the code to make sure the changes in the Application.e4xmi where picked up? – greg-449 Mar 25 '19 at 09:51
  • Yes i have this argument in my run configuration. – Darksmilie Mar 25 '19 at 10:19
  • I still get this null pointer for the command. What can be the reason that the command service cannot create this command? Are there certain conditions that have to be given to create a command with the command service? – Darksmilie Apr 08 '19 at 10:07
  • As far as I can see the command service **never** returns null from createCommand even when the command id is nonsense. So do some debugging and find out exactly what is null. – greg-449 Apr 08 '19 at 10:29

0 Answers0