4

I am developing e4 application. I want to inject EPartService outside the Part and Handler when i am injecting EPartService then i will get null pointer error

  public class DisplayRuntimePart {
          @Inject  EPartService partService;
          private void displayPart(){
          MPart part=partService.findPart("com.rcpe4.myproject.part.datapart");                   
          mpart.setVisible(true);
          partService.showPart(mpart, PartState.CREATE);   
    }
  }

I am also read this question but till not solve my problem E4 EPartService findPart() throwing java.lang.Null Pointer Exception

Edit I am inject EPartService in Part class. Class URI in Application.e4xml is bundleclass://com.abc.test/com.abc.test.part.MyPart in this class I am write code as follows.

      Class Mypart{ 
          @Inject EPartService prtservice;                  
          @Inject
          public MyPart() {
          } 
          @PostConstruct
          public void postConstruct(Composite parent) {
             parent.setLayout(new  FillLayout(SWT.HORIZONTAL));     
             htmlBrowser = new Browser(parent, SWT.NONE);

         }  
         @PreDestroy
        public void preDestroy() {

        }   
        @Focus
        public void onFocus() {

        }   
        @Persist
        public void save() {

        } 
         public dispalyPart(){  
                MPart mpart=partService.findPart("com.abc.test.part.datapart"); **Here Getting Null Pointer Exception**
                mpart.setVisible(true);
                partService.showPart(mpart, PartState.CREATE);  
        }
   }
Abhit
  • 481
  • 6
  • 19

1 Answers1

1

Eclipse only does direct injection on objects that it 'knows' about - basically objects mentioned in the application model (e4xmi) files or created using something like EPartService.showPart.

If you want to do direct injection on objects that you create then you need to create them using ContextInjectionFactory. For example:

@Inject IEclipseContext context;

...

MyClass myClass = ContextInjectionFactory.make(MyClass.class, context);

you can also do injection on a class created in the normal way with:

ContextInjectionFactory.inject(myClass, context);

(this will not do injection on the constructor).

Note: Since this code is using direct injection you must run it from a class that the Eclipse application model does know about such as a command handler or an MPart.

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • When i am @Inject IEclipseContext context; context object give null pointer exception – Abhit Sep 10 '14 at 07:54
  • The code sample DisplayRuntimePart myClass = ContextInjectionFactory.make(DisplayRuntimePart.class, context); MPart mpart=partService.findPart("com.rcpe4.myproject.part.datapart"); mpart.setVisible(true); partService.showPart(mpart, PartState.CREATE); – Abhit Sep 10 '14 at 07:56
  • You must run this code in a class which is created by Eclipse from the application model so that the injection will work. So a MPart or a Handler or something like that. – greg-449 Sep 10 '14 at 12:27
  • Hi greg-449 I am tried implementing code in Part class as per you say please see the edited code. but still it give null pointer exception – Abhit Sep 11 '14 at 05:01
  • Where does it throw the exception? What is null? – greg-449 Sep 11 '14 at 06:42
  • partService reference gives null value in dispalyPart() method when I call this method – Abhit Sep 11 '14 at 07:35
  • Are you doing a `new Mypart()`? – greg-449 Sep 11 '14 at 07:40
  • greg-449 No, i am not use new MyPart(); i will call displayPart method from other mehod of MyPart – Abhit Sep 11 '14 at 08:00
  • You could check in the @PostConstruct that the part service has been injected. Then check that you are using the same instance of the class when displayPart is called. – greg-449 Sep 11 '14 at 14:19