2014-07-10 62 views
4

這是問題所在。我使用嚮導創建了一個示例Eclipse 4(Luna)應用程序。一切正常。我決定將用戶界面移至專用插件。我創建了新插件,將所有類移至新插件,並重構了Eclipse應用程序模型,產品和插件配置。現在當我嘗試啓動Eclipse產品時,我得到了這個依賴注入異常。當我調試,我注意到,所有的UI組件變量具有空值:org.eclipse.e4.core.di.InjectionException:java.lang.NullPointerException

package my.company.ui.parts; 

import javax.annotation.PostConstruct; 
import javax.inject.Inject; 

import org.eclipse.e4.ui.di.Focus; 
import org.eclipse.e4.ui.di.Persist; 
import org.eclipse.e4.ui.model.application.ui.MDirtyable; 
import org.eclipse.jface.viewers.TableViewer; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.ModifyEvent; 
import org.eclipse.swt.events.ModifyListener; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Text; 

public class SamplePart { 

    private Text txtInput; 
    private TableViewer tableViewer; 

    @Inject 
    private MDirtyable dirty; 

    @PostConstruct 
    public void createComposite(Composite parent) { 
     parent.setLayout(new GridLayout(1, false)); 

     txtInput = new Text(parent, SWT.BORDER); 
     txtInput.setMessage("Enter text to mark part as dirty"); 
     txtInput.addModifyListener(new ModifyListener() { 
      @Override 
      public void modifyText(ModifyEvent e) { 
       dirty.setDirty(true); 
      } 
     }); 
     txtInput.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 

     tableViewer = new TableViewer(parent); 

     tableViewer.add("Sample item 1"); 
     tableViewer.add("Sample item 2"); 
     tableViewer.add("Sample item 3"); 
     tableViewer.add("Sample item 4"); 
     tableViewer.add("Sample item 5"); 
     tableViewer.getTable().setLayoutData(new GridData(GridData.FILL_BOTH)); 
    } 

    @Focus 
    public void setFocus() { 
     tableViewer.getTable().setFocus(); 
    } 

    @Persist 
    public void save() { 
     dirty.setDirty(false); 
    } 
} 

拋出異常setFocus()方法,因爲tableViewer變量的值是零。這裏的堆棧跟蹤:

!ENTRY org.eclipse.e4.ui.workbench 4 0 2014-07-10 16:40:26.368 
!MESSAGE Failed to grant focus to element 
!STACK 0 
org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException 
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:62) 
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247) 
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:225) 
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:107) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.focusGui(PartRenderingEngine.java:795) 
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.activate(PartServiceImpl.java:679) 
    at org.eclipse.e4.ui.internal.workbench.PartServiceImpl.switchPerspective(PartServiceImpl.java:572) 
    at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.showTab(PerspectiveStackRenderer.java:127) 
    at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer$1.handleEvent(LazyStackRenderer.java:66) 
    at org.eclipse.e4.ui.services.internal.events.UIEventHandler$1.run(UIEventHandler.java:40) 
    at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:187) 
    at org.eclipse.swt.widgets.Display.syncExec(Display.java:4734) 
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application$1.syncExec(E4Application.java:218) 
    at org.eclipse.e4.ui.services.internal.events.UIEventHandler.handleEvent(UIEventHandler.java:36) 
    at org.eclipse.equinox.internal.event.EventHandlerWrapper.handleEvent(EventHandlerWrapper.java:197) 
    at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:197) 
    at org.eclipse.equinox.internal.event.EventHandlerTracker.dispatchEvent(EventHandlerTracker.java:1) 
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230) 
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:148) 
    at org.eclipse.equinox.internal.event.EventAdminImpl.dispatchEvent(EventAdminImpl.java:135) 
    at org.eclipse.equinox.internal.event.EventAdminImpl.sendEvent(EventAdminImpl.java:78) 
    at org.eclipse.equinox.internal.event.EventComponent.sendEvent(EventComponent.java:39) 
    at org.eclipse.e4.ui.services.internal.events.EventBroker.send(EventBroker.java:81) 
    at org.eclipse.e4.ui.internal.workbench.UIEventPublisher.notifyChanged(UIEventPublisher.java:59) 
    at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:374) 
    at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:135) 
    at org.eclipse.e4.ui.model.application.ui.advanced.impl.PerspectiveStackImpl.setSelectedElement(PerspectiveStackImpl.java:1) 
    at org.eclipse.e4.ui.workbench.renderers.swt.LazyStackRenderer.postProcess(LazyStackRenderer.java:103) 
    at org.eclipse.e4.ui.workbench.renderers.swt.PerspectiveStackRenderer.postProcess(PerspectiveStackRenderer.java:77) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:678) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:766) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:737) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:731) 
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:715) 
    at org.eclipse.e4.ui.workbench.renderers.swt.SWTPartRenderer.processContents(SWTPartRenderer.java:68) 
    at org.eclipse.e4.ui.workbench.renderers.swt.WBWRenderer.processContents(WBWRenderer.java:664) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:674) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.safeCreateGui(PartRenderingEngine.java:766) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.access$2(PartRenderingEngine.java:737) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$7.run(PartRenderingEngine.java:731) 
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.createGui(PartRenderingEngine.java:715) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1074) 
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) 
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032) 
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148) 
    at org.eclipse.e4.ui.internal.workbench.swt.E4Application.start(E4Application.java:164) 
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196) 
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134) 
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104) 
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:382) 
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:236) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648) 
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603) 
    at org.eclipse.equinox.launcher.Main.run(Main.java:1465) 
    at org.eclipse.equinox.launcher.Main.main(Main.java:1438) 
Caused by: java.lang.NullPointerException 
    at com.boeing.ccts.mpa.workbench.planner.ui.parts.SamplePart.setFocus(SamplePart.java:52) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:483) 
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55) 
    ... 60 more 

我在另一篇文章是有事情做與這也導致相信這是一個依賴注入問題的註釋讀取。不知道如何解決它。任何幫助,將不勝感激。

+0

正在運行@PostConstruct方法嗎?你在新插件中使用了模型片段還是在主應用程序模型中使用了一切? –

+0

我只是想通了。在'@ PostConstruct'方法中拋出異常。它正在運行,但所有變量均爲空,因爲DI不工作(構造函數的父窗口值爲空)。我用'@Inject'取代了'@ PostConstruct'註解,並解決了這個問題。感謝您的幫助greg-449 - 非常感謝。 – AndyB

回答

0

檢查是否有不同版本的javax.annotation正在運行。您可以使用OSGi控制檯並命令packages javax.annotation來驗證這是否適用。我曾經遇到類似於你看到的效果的問題:兩個bundle使用不同版本的註釋,因此不會識別其他版本中使用的註釋,因爲不同的bundle有不同的類加載器。

相關問題