Bug 212096 - LocalVariable.hashCode throws NPE when this.parent is null
Summary: LocalVariable.hashCode throws NPE when this.parent is null
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.4   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: 3.4 M6   Edit
Assignee: Jerome Lanneluc CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-12-05 20:13 EST by Robert Konigsberg CLA
Modified: 2008-04-25 13:18 EDT (History)
2 users (show)

See Also:


Attachments
Proposed fix and regression test (2.95 KB, patch)
2007-12-13 10:23 EST, Jerome Lanneluc CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Konigsberg CLA 2007-12-05 20:13:34 EST
I'm trying to store IJavaElements in a HashTable. I'm finding that some elements are throwing NPE while generating the hashCode because the LocalVariable object is initialized with a null parent. Exact reproduction of this case may be difficult
Comment 1 Jerome Lanneluc CLA 2007-12-07 11:30:19 EST
Looking at the code, I don't see how a local variable with a null parent can be created. How is the local variable obtained? (i.e what API is used?)
Comment 2 Robert Konigsberg CLA 2007-12-07 11:38:30 EST
Ah that's fair.

Perhaps you can help me out. I can reliably reproduce this on my workstation, but I don't know that I can reasonably, for instance, ship you the workspace, given that it's got copyrighted material.

I have set a conditional breakpoint in LocalVariable's ctor that engages only when parent is null. I can do it again. The easy thing I can do is send you part of the stack trace. I'll hunt down a little more.
Comment 3 Jerome Lanneluc CLA 2007-12-07 12:03:40 EST
Yes, providing such a stack trace can help identifying the problem. 
Comment 4 Robert Konigsberg CLA 2007-12-07 13:13:09 EST
Jerome, here is some information. I suspect the nature of the anonymous class will be helpful.

Stack trace:
LocalVariable.<init>(JavaElement, String, int, int, int, int, String) line: 42	
VariableBinding.getUnresolvedJavaElement() line: 222	
VariableBinding.getJavaElement() line: 132	
(The caller)JavaTypeFinder$1.resolveName(Name) line: 278	

In VaribleBinding.getUnresolvedJavaElement, is where the null value comes from. It's the local variable's method.
  Method comes from (as you would know) declartingMethod.getJavaElement().
  MethodBinding.getJavaElement calls MethodBinding.getUnresolvedJavaElement
  It calls (IType)getDeclaringClass.getJavaElement(). This returns null.

  FYI, the toString of the declaring class is:
  Anonymous type : (id=NoId)
final class $Local$
	extends java.util.TimerTask
	enclosing type : MyClass
/*   methods   */
void <init>() 
void run() 

Does this help?
Comment 5 Jerome Lanneluc CLA 2007-12-11 05:12:15 EST
Unfortunately the line numbers don't match with my version (I assumed you were using one of the latest 3.4 build as indicated in the Version field).
What is your Eclipse build number?
Comment 6 Robert Konigsberg CLA 2007-12-11 12:53:30 EST
3.3.0. Build id: I20070625-1500
Comment 7 Jerome Lanneluc CLA 2007-12-13 10:17:58 EST
Thanks. I think this can happen only if the compilation unit is changed (or the working copy is discarded) after the binding was obtained.
Comment 8 Jerome Lanneluc CLA 2007-12-13 10:23:58 EST
Created attachment 85186 [details]
Proposed fix and regression test
Comment 9 Robert Konigsberg CLA 2007-12-13 14:11:21 EST
I think this might not be what's happening to me. You can help me verify this: Can you identify a breakpoint that would fire when the compilation unit is changed (or working copy discarded)? If you can do that, I can set it and test.
Comment 10 Jerome Lanneluc CLA 2007-12-14 03:53:11 EST
A org.eclipse.jdt.internal.core.CompilationUnit can be changed when the following methods are called:
- makeConsistent(*)
- reconcile(*)

A working copy is discarded when org.eclipse.jdt.internal.core.CompilationUnit.discardWorkingCopy() is called.
Comment 11 Jerome Lanneluc CLA 2008-02-01 10:36:54 EST
> I think this might not be what's happening to me.
Robert, do you have more information on your scenario ?
Did you try the patch from comment 8 ? if so, does it fix your problem ?
Comment 12 Robert Konigsberg CLA 2008-02-14 11:48:27 EST
Sorry for the delay. I haven't tried this, I don't really know how I can apply the patch and run it against my environment. If there is a build against which I can run this, I can try to verify it.
Comment 13 Jerome Lanneluc CLA 2008-02-15 05:41:39 EST
Fix and test released for 3.4M6.
This fix will be in next week's integration build (it should be I20080219-0800).
Please reopen this bug if you still have the problem with this build.
Comment 14 David Audel CLA 2008-03-25 08:35:00 EDT
Verified for 3.4M6 using build I20080324-1300.
Comment 15 Robert Konigsberg CLA 2008-04-25 13:18:02 EDT
I have downloaded 3.4M6 and, set breakpoints and will let you know if it reoccurs. Thank you for your help.