我正在尝试在eclipse插件开发中检测用户的选择时遇到错误“NullPointExeption”

时间:2014-01-06 13:27:41

标签: java eclipse eclipse-plugin

我在尝试检测用户在eclipse插件开发中的选择时遇到错误“NullPointExeption”。我已经为这两个类附加了代码,首先是DetectSelection,它检测用户在项目浏览器中的选择,第二个是SampleView,它生成自定义插件

我之前询问的问题:Eclipse Plugin: Get the class name on user's click/selection

之后它运行正常,但现在我又面临一些错误: 现在,我创建了DetectSelection类:

public class DetectSelection
 {
String classname ;
IPath packageName ;
private ISelection selection;

public void setSelection(ISelection iSelection) 
{
    this.selection = iSelection;
}

public void jk() 
{
    System.out.println(" HI!!!!");
    IWorkbenchPage page =PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
    IViewPart viewPart = page.findView("org.eclipse.jdt.ui.PackageExplorer");
    ISelectionProvider selProvider = viewPart.getSite().getSelectionProvider();
    selProvider.addSelectionChangedListener(new ISelectionChangedListener()
    {
        public void selectionChanged(SelectionChangedEvent event) 
        {
            if (!event.getSelection().isEmpty()) 
            {
                Iterator ite = ((IStructuredSelection) event.getSelection()).iterator();
                while (ite.hasNext()) 
                {
                    Object obj = ite.next();
                    String testString="";
                    if (obj instanceof IJavaElement) 
                    {
                        packageName= ((IJavaElement)obj).getPath();
                        System.out.println("getFileExtension()   "+ packageName.getFileExtension() );
                        String s= packageName.toString();
                        String s1[]= s.split("/");
                        if ("java".equals(packageName.getFileExtension()))
                        {
                            System.out.println("package name "+ s1[1]);
                            System.out.println("project name"+ ((IJavaElement)obj).getElementName());
                        }
                    }
                }
            }
        }
    });

 }
 }

以前工作正常,当我在SampleView类中创建了一个这样的对象并使用它时:

 public class SampleView extends ViewPart 
 {
public static final String ID = "typesview.views.SampleView";
private TableViewer viewer;
private Action action1;
private Action action2;
private Action doubleClickAction;

class ViewContentProvider implements IStructuredContentProvider
{
    public void inputChanged(Viewer v, Object oldInput, Object newInput) 
    {
    }
    public void dispose() 
    {
    }
    public Object[] getElements(Object parent) 
    {
        return new String[] { "One", "Two", "Three" };
    }
}
class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
    public String getColumnText(Object obj, int index) {
        return getText(obj);
    }
    public Image getColumnImage(Object obj, int index) {
        return getImage(obj);
    }
    public Image getImage(Object obj) {
        return PlatformUI.getWorkbench().
                getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
    }
}
class NameSorter extends ViewerSorter {
}

public SampleView() {
}

public void createPartControl(Composite parent) {
    **DetectSelection obj2 = new DetectSelection();
    obj2.jk();**
    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
    viewer.setContentProvider(new ViewContentProvider());
    viewer.setLabelProvider(new ViewLabelProvider());
    viewer.setSorter(new NameSorter());
    viewer.setInput(getViewSite());

    PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "TYPESview.viewer");
    makeActions();
    hookContextMenu();
    hookDoubleClickAction();
    contributeToActionBars();
}
private void hookContextMenu() {
    MenuManager menuMgr = new MenuManager("#PopupMenu");
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.addMenuListener(new IMenuListener() {
        public void menuAboutToShow(IMenuManager manager) {
            SampleView.this.fillContextMenu(manager);
        }
    });
    Menu menu = menuMgr.createContextMenu(viewer.getControl());
    viewer.getControl().setMenu(menu);
    getSite().registerContextMenu(menuMgr, viewer);
}

private void contributeToActionBars() {
    IActionBars bars = getViewSite().getActionBars();
    fillLocalPullDown(bars.getMenuManager());
    fillLocalToolBar(bars.getToolBarManager());
}

private void fillLocalPullDown(IMenuManager manager) {
    manager.add(action1);
    manager.add(new Separator());
    manager.add(action2);
}

private void fillContextMenu(IMenuManager manager) {
    manager.add(action1);
    manager.add(action2);
    manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
}

private void fillLocalToolBar(IToolBarManager manager) {
    manager.add(action1);
    manager.add(action2);
}

private void makeActions() {
    action1 = new Action() {
        public void run() {
            showMessage("Action 1 executed");
        }
    };
    action1.setText("Action 1");
    action1.setToolTipText("Action 1 tooltip");
    action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
        getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

    action2 = new Action() {
        public void run() {
            showMessage("Action 2 executed");
        }
    };
    action2.setText("Action 2");
    action2.setToolTipText("Action 2 tooltip");
    action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().
            getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
    doubleClickAction = new Action() {
        public void run() {
            ISelection selection = viewer.getSelection();
            Object obj = ((IStructuredSelection)selection).getFirstElement();
            showMessage("Double-click detected on "+obj.toString());
        }
    };
}

private void hookDoubleClickAction() {
    viewer.addDoubleClickListener(new IDoubleClickListener() {
        public void doubleClick(DoubleClickEvent event) {
            doubleClickAction.run();
        }
    });
}
private void showMessage(String message) {
    MessageDialog.openInformation(
        viewer.getControl().getShell(),
        "Sample View",
        message);
}

public void setFocus() {
    viewer.getControl().setFocus();
}
}

现在运行插件时,我收到错误:

java.lang.NullPointerException
at typesview.views.DetectSelection.jk(DetectSelection.java:40)
at typesview.views.SampleView.createPartControl(SampleView.java:58)
at org.eclipse.ui.internal.ViewReference.createPartHelper(ViewReference.java:375)
at org.eclipse.ui.internal.ViewReference.createPart(ViewReference.java:229)
at org.eclipse.ui.internal.WorkbenchPartReference.getPart(WorkbenchPartReference.java:595)
at org.eclipse.ui.internal.WorkbenchPage$ActivationList.setActive(WorkbenchPage.java:4317)
at org.eclipse.ui.internal.WorkbenchPage$18.runWithException(WorkbenchPage.java:3359)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.application.WorkbenchAdvisor.openWindows(WorkbenchAdvisor.java:803)
at org.eclipse.ui.internal.Workbench$33.runWithException(Workbench.java:1600)
at org.eclipse.ui.internal.StartupThreading$StartupRunnable.run(StartupThreading.java:31)
at org.eclipse.swt.widgets.RunnableLock.run(RunnableLock.java:35)
at org.eclipse.swt.widgets.Synchronizer.runAsyncMessages(Synchronizer.java:135)
at org.eclipse.swt.widgets.Display.runAsyncMessages(Display.java:4140)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3757)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2609)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2499)
at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:679)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:668)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:123)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:344)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:622)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:577)
at org.eclipse.equinox.launcher.Main.run(Main.java:1410)
at org.eclipse.equinox.launcher.Main.main(Main.java:1386)

获取行号的图片:

DetectSelection: enter image description here

SampleView: enter image description here

1 个答案:

答案 0 :(得分:1)

堆栈跟踪和屏幕截图中的行号不能正确排列。

但实际上,异常只能通过jk方法的前五行抛出。 (其余的是定义一个回调方法/对象,如果该代码抛出了NPE,堆栈跟踪中的类名将会不同。)

我建议你添加更多的跟踪打印,或者使用调试器来确定实际抛出异常的哪个语句...然后向后工作到{{1}的源代码}。


该方法的javadoc说:

  

返回此页面中具有指定ID的视图。页面中最多只有一个具有指定ID的视图。

     

返回:视图,如果没有找到则返回null

如果您从原因中获得null,则表明id参数不正确。

这可能相关或不相关,但您将null传递给"org.eclipse.jdt.ui.PackageExplorer",但find课程的IDSampleView。< / p>