Bug 311022 - NPE in InternalExtendedCompletionContext.getVisibleElement
Summary: NPE in InternalExtendedCompletionContext.getVisibleElement
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.7 M3   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 342330 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-04-29 09:36 EDT by Johannes Rieken CLA
Modified: 2011-04-26 12:53 EDT (History)
4 users (show)

See Also:
Olivier_Thomann: review+


Attachments
proposed fix v1.0 + regression tests (6.00 KB, patch)
2010-10-07 09:58 EDT, Ayushman Jain CLA
no flags Details | Diff
proposed fix v1.0 (small correction) + regression tests (6.00 KB, patch)
2010-10-07 10:03 EDT, Ayushman Jain CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Johannes Rieken CLA 2010-04-29 09:36:34 EDT
Build Identifier: 3.6M6

null
Error
Thu Apr 29 12:46:40 CEST 2010
Unhandled event loop exception

java.lang.NullPointerException
at org.eclipse.jdt.internal.codeassist.InternalExtendedCompletionContext.getVisibleElements(InternalExtendedCompletionContext.java:351)
at org.eclipse.jdt.internal.codeassist.InternalCompletionContext.getVisibleElements(InternalCompletionContext.java:318)
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.getAssignableElements(ParameterGuessingProposal.java:110)
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.guessParameters(ParameterGuessingProposal.java:290)
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.computeGuessingCompletion(ParameterGuessingProposal.java:225)
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.computeReplacementString(ParameterGuessingProposal.java:191)
at org.eclipse.jdt.internal.ui.text.java.LazyJavaCompletionProposal.getReplacementString(LazyJavaCompletionProposal.java:308)
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.apply(AbstractJavaCompletionProposal.java:369)
at org.eclipse.jdt.internal.ui.text.java.JavaMethodCompletionProposal.apply(JavaMethodCompletionProposal.java:55)
at org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal.apply(ParameterGuessingProposal.java:120)
at org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal.apply(AbstractJavaCompletionProposal.java:482)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertProposal(CompletionProposalPopup.java:928)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.insertSelectedProposalWithMask(CompletionProposalPopup.java:879)
at org.eclipse.jface.text.contentassist.CompletionProposalPopup.verifyKey(CompletionProposalPopup.java:1305)
at org.eclipse.jface.text.contentassist.ContentAssistant$InternalListener.verifyKey(ContentAssistant.java:806)
at org.eclipse.jface.text.TextViewer$VerifyKeyListenersManager.verifyKey(TextViewer.java:489)
at org.eclipse.swt.custom.StyledTextListener.handleEvent(StyledTextListener.java:65)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3725)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1333)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1356)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1341)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1155)
at org.eclipse.swt.custom.StyledText.handleKeyDown(StyledText.java:5944)
at org.eclipse.swt.custom.StyledText$7.handleEvent(StyledText.java:5643)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:3725)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1333)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1356)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1341)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1370)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1366)
at org.eclipse.swt.widgets.Canvas.sendKeyEvent(Canvas.java:416)
at org.eclipse.swt.widgets.Control.doCommandBySelector(Control.java:957)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5005)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
at org.eclipse.swt.internal.cocoa.NSResponder.interpretKeyEvents(NSResponder.java:56)
at org.eclipse.swt.widgets.Composite.keyDown(Composite.java:543)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4915)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
at org.eclipse.swt.widgets.Widget.callSuper(Widget.java:221)
at org.eclipse.swt.widgets.Widget.windowSendEvent(Widget.java:1909)
at org.eclipse.swt.widgets.Shell.windowSendEvent(Shell.java:1994)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4977)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
at org.eclipse.swt.widgets.Display.applicationSendEvent(Display.java:4527)
at org.eclipse.swt.widgets.Display.applicationProc(Display.java:4604)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
at org.eclipse.swt.internal.cocoa.NSApplication.sendEvent(NSApplication.java:115)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3238)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2416)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2380)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2229)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:504)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:497)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
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:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at org.eclipse.equinox.launcher.Main.run(Main.java:1406)



Reproducible: Sometimes
Comment 1 Olivier Thomann CLA 2010-04-29 10:21:44 EDT
Any steps to reproduce.
Comment 2 Olivier Thomann CLA 2010-04-29 10:23:55 EDT
The NPE occurs on the line:
if (assignableTypeBinding != null && !binding.type.isCompatibleWith(assignableTypeBinding)) continue next;
Comment 3 Johannes Rieken CLA 2010-04-30 04:09:54 EDT
Sorry, no steps. I just found them 2-3 times in my error log.
Comment 4 Ayushman Jain CLA 2010-09-20 08:17:25 EDT
Since this cant be reproduced as of now, I'm tempted to close this as WORKSFORME. Please report with steps if this can be reproduced. Thanks!
Comment 5 Ayushman Jain CLA 2010-09-28 10:08:00 EDT
No steps yet. Closing as WORKSFORME. Please reopen if you have steps.
Comment 6 Johannes Rieken CLA 2010-09-28 11:26:52 EDT
(In reply to comment #5)
> No steps yet. Closing as WORKSFORME. Please reopen if you have steps.

That's disappointing. According to comment 2 there are not may ways to fail with a NPE.
Comment 7 Ayushman Jain CLA 2010-09-28 12:33:17 EDT
(In reply to comment #6)
> (In reply to comment #5)
> > No steps yet. Closing as WORKSFORME. Please reopen if you have steps.
> 
> That's disappointing. According to comment 2 there are not may ways to fail
> with a NPE.

Thats correct. Yet, without any reproducible scenario, I dont think just adding a guard against null on that statement would be sufficient. The problem might be occuring because of some incorrect parameter passed somewhere, and might be in a completely different place altogether. Adding a guard here(which may not even be required at all) would no doubt prevent an NPE, but we'll end up with no proposals. Its better to reproduce the issue and fix the root cause IMO.
Comment 8 Markus Keller CLA 2010-10-04 13:27:02 EDT
Adding a null guard without knowing what's going on would indeed be wrong. But another approach to resolve the issue is to read the surrounding code and then make an informed guess about the circumstances that could cause an NPE there.

My guess was that the type of the variable cannot be resolved due to a compile error, so I tried to create some broken code where that could be the case.

To reproduce, invoke content assist after "foo" on the last line of bar() here:

package p;
class Bug {
	void foo(Object o) { }
	
	<T> void bar() {
		T<T> loc= 12;
		foo
	}
}
Comment 9 Ayushman Jain CLA 2010-10-05 01:37:37 EDT
Thanks Markus. I'll investigate.
Comment 10 Ayushman Jain CLA 2010-10-07 05:07:29 EDT
(In reply to comment #9)
> Thanks Markus. I'll investigate.

The problem comes in only some peculiar cases where we return null as the resolved type for the binding because of erroneous code, such as when hierarchy cycle is detected, incorrect arity for parameterized type, when non-generic type is parameterized (shown by above example), etc. (See org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference.internalResolveType(Scope, ReferenceBinding, boolean) ).
In other case such as these:
    void bar() {
        Zar loc= 12;   // unknown type Zar
        foo
    }
we return a missing type binding, which saves the NPE. 

So in this case, the best approach is to add a null check for binding.type on the line if (assignableTypeBinding != null &&
!binding.type.isCompatibleWith(assignableTypeBinding)) continue next;

Working on a patch with tests.
Comment 11 Ayushman Jain CLA 2010-10-07 06:02:56 EDT
Another test
package p;
class Bug<A1,A2> {
    void foo(Object o) { }
 
    void bar() {
    	Bug<String,String,String> bug;
        foo|
    }
}
Comment 12 Ayushman Jain CLA 2010-10-07 09:58:04 EDT
Created attachment 180424 [details]
proposed fix v1.0 + regression tests

Olivier, please review. TIA
Comment 13 Ayushman Jain CLA 2010-10-07 10:03:28 EDT
Created attachment 180425 [details]
proposed fix v1.0 (small correction) + regression tests

Just added brackets to make the meaning more clear
Comment 14 Olivier Thomann CLA 2010-10-07 10:04:33 EDT
Patch looks good.
Comment 15 Ayushman Jain CLA 2010-10-07 10:12:31 EDT
Released in HEAD for 3.7M3.
Comment 16 Johannes Rieken CLA 2010-10-07 11:42:24 EDT
Thanks everyone.
Comment 17 Olivier Thomann CLA 2010-10-26 14:43:57 EDT
Verified for 3.7M3 using I20101025-1602 (4.1 build)
Comment 18 Markus Keller CLA 2011-04-26 11:38:09 EDT
*** Bug 342330 has been marked as a duplicate of this bug. ***