简体   繁体   English

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

[英]I am facing error “NullPointExeption”, while trying to detect the user's selection in the eclipse plugin development

I am facing error "NullPointExeption", while trying to detect the user's selection in the eclipse plugin development. 我在尝试在eclipse插件开发中检测用户的选择时遇到错误“ NullPointExeption”。 I have attached the code for both the classes, first, DetectSelection which detects the user's choice in the project explorer and second, SampleView which makes the custom plugin 我已经附上了两个类的代码,首先是DetectSelection,它检测项目浏览器中的用户选择,其次,是SampleView,它使自定义插件

Question that I asked Before : Eclipse Plugin: Get the class name on user's click/selection 我之前问过的问题: Eclipse插件:在用户单击/选择时获取类名

After that it was working properly, but now I am again facing some errors: Now, I made DetectSelection class : 之后,它可以正常工作,但是现在我又遇到了一些错误:现在,我创建了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());
                        }
                    }
                }
            }
        }
    });

 }
 }

This was working fine previously, when I made an object of this in the SampleView class and used it like this: 以前,当我在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();
}
}

Now on running the plugin, I am getting the error: 现在,在运行插件时,出现错误:

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)

Images to get the line numbers: 图片获取行号:

DetectSelection: DetectSelection: 在此处输入图片说明

SampleView: SampleView: 在此处输入图片说明

The line numbers in the stacktrace and the screen-shot don't line up properly. stacktrace和屏幕截图中的行号未正确对齐。

But in fact, the exception can only be thrown by the first five lines of the jk method anyway. 但是实际上,无论如何,只能由jk方法的前五行抛出异常。 (The rest is defining a callback method / object, and if an NPE was thrown by that code, the classname in the stack trace would be different.) (其余的定义了一个回调方法/对象,如果该代码抛出了NPE,则堆栈跟踪中的类名将有所不同。)

I suggest that you add some more trace-prints, or use the debugger to figure out which statement is actually throwing the exception ... and work backwards to the source of the null . 我建议您添加更多的跟踪记录,或使用调试器找出哪条语句实际上引发了异常...并向后返回null的源。


The javadoc for that method says: 该方法的javadoc说:

Returns the view in this page with the specified id. 返回此页面中具有指定ID的视图。 There is at most one view in the page with the specified id. 页面中最多有一个具有指定ID的视图。

Returns: the view, or null if none is found 返回:视图;如果未找到,则返回null

If you are getting a null from the cause, it follows that the id parameter is not correct. 如果从原因中得到null ,则说明id参数不正确。

This may or may not be relevant, but you are passing "org.eclipse.jdt.ui.PackageExplorer" to find , but the ID for your SampleView class is "typesview.views.SampleView" . 这可能相关,也可能无关,但是您正在传递"org.eclipse.jdt.ui.PackageExplorer"find ,但是SampleView类的ID"typesview.views.SampleView"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使Restful Webservice及其客户端成为传递XML时遇到的问题 - I am trying to make Restful Webservice and it's client,facing issue while passing XML 我正在开发 php docker 应用程序。尝试 docker-compose up 命令时遇到错误 - I am working on a php docker application.Am facing an error while trying docker-compose up command 尝试运行Eclipse插件时出错 - Error while trying to run the Eclipse plugin Eclipse插件:在用户单击/选择时获取类名 - Eclipse Plugin: Get the class name on user's click/selection 当我尝试安装黄瓜Eclipse插件时,它没有安装并显示一些错误 - When I am trying to install cucumber Eclipse Plugin then it is not installing and showing some error 尝试通过Eclipse数据库开发透视图连接到数据库时出错 - Error while trying to connect to database through eclipse database development perspective 我正在尝试在 Eclipse 中使用 Swing 但我不断收到此错误。 见下文 - I am trying to user Swing in Eclipse but I keep getting this error. See below 我在 Pascal 的三角形代码中面临的逻辑错误是什么? - What is the logical error I am facing in Pascal's Triangle code? 当我尝试运行 appium 自动化项目时,方法 getBinaryPath() 未定义在 eclipse 中显示的类型 WebDriverManager 错误 - The method getBinaryPath() is undefined for the type WebDriverManager error shows in eclipse while i am trying to run appium automation project 尝试在Eclipse中安装Mase插件时出现错误消息 - Error message while trying to install the mase plugin in eclipse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM