why would psielement . geparent return null?

in otherwords,  what implementation "has no parent", the presumed meaning of returning null here, and what does such an iplementation imply about that psielement?

3 comments
Comment actions Permalink

There are two cases. One is rare and OK: when that PsiElement is actually a PsiDirectory corresponding to a disk directory which really has no parent. Another, more frequently occurring case, is when that PsiElement has been deleted and is not valid anymore. See PsiElement.isValid.

0
Comment actions Permalink

Thank you ! ResolveResult returns a PsiElement from its getElement method. In some implementations of this interface, it,s possible that null will be returned- CandidateInfo for example, doez not preclude the constructor parameter that serves as a return value from getElement from being null.


In class ControlFlowUtil, in method checkReferenceExpressionScope a boolean is returned, and one way for that return value boolean to be decided is if the return value froma call to ReturnResult. getElement()  (actually, a declared-type JavaReturnResult) is null.

So there the nullness of a PsiElement is being used to decide , effectively, if a code fragment in an anonymous inner class can be extracted to the outside claass. (My understanding of the semantics of checkReferenceExpressionScope) . So given what you said, I am wondering if methods which return PsiElements also abide by the null== file || null == something changed semantics which you you outlined or if PsiElement== null in this case has different semantics.

The motivation for this question is that it is not clear to me how the nullness of a PsiElement can serve as an affirmation that a code frgament in an anonymous inner class can successfully be extracted to the containing member, presumably the outer class. I can cite the specific lines where this is what's happening.

I should append to this that , when getElement returns null, that null value ultimately had issued from a call to PsiElement.getParent () about 8 or 10 calls down the call stack. Unfortunately, I don't have the source available to give you the exact line, but it's not important. The point is that a call to getParent results in null and that null is then propagated up the call stack where it ultimately comes to be used to decide the return value of the aforementioned method. Of course there may be other meanings implied by the nullness of Psi.getElement() in this method whivh did not invovle a previous call to getParent(), but actually, that doesn't clear much up in this case and at least it seems to be the case that getParent is one possible source of null.

Message was edited by: java developer to increase clarity and give context.

0
Comment actions Permalink

ResolveResult.getElement being null is a completely normal situation. If you have a reference with an undefined name and call advancedResolve on it, you'll get JavaResolveResult.EMPTY which returns exactly null. It's a synonym of PsiReference.resolve() returning null, and it only says that IDEA can't resolve a reference. This happens very often while editing code. Automatic refactorings over code with such references are unlikely to be meaningful.

It' hard to tell how a result of some element.getParent() would end up in that resolve result. In fact, it sounds pretty suspicious, might be a bug. So if you can reproduce this and provide the details like what element it was, we'd be very grateful.

0

Please sign in to leave a comment.