Skip to content

Comments

Fix NPE on goto to invalid method reference#9210

Merged
mbien merged 1 commit intoapache:masterfrom
mbien:goto-invalid-method-reference
Feb 21, 2026
Merged

Fix NPE on goto to invalid method reference#9210
mbien merged 1 commit intoapache:masterfrom
mbien:goto-invalid-method-reference

Conversation

@mbien
Copy link
Member

@mbien mbien commented Feb 17, 2026

  • check if element is a record before listing components
  • removed reflective method calls and updated logic
  • added regression test

reproducer:

public class GoToInvalidMethodReferenceNPE {
    public static void main(String args[]) {
        ChangeListener listener = GoToInvalidMethodReferenceNPE::invalid;
        //                                                       ^^^^^^^ ctrl click (after parsing)
    }
}

fixes #8735

@mbien mbien added this to the NB30 milestone Feb 17, 2026
@mbien mbien requested a review from lahodaj February 17, 2026 04:49
@mbien mbien added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) Editor ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) labels Feb 17, 2026
Comment on lines 482 to 483
if (peer.getKind() == ElementKind.RECORD_COMPONENT) {
for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this cast ok?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it mostly is OK. And it basically weakly suggests that peer.getEnclosingElement() == enclosing, which should be always true. (I probably would just do enclosing.getEnclosingElement(), though.)

I wonder if it is possible to write a test for this - I can try at some point if needed.

Otherwise - great cleanup, thanks for doing this!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I probably would just do enclosing.getEnclosingElement(), though.)

you mean changing

    for (RecordComponentElement component : ((TypeElement)peer.getEnclosingElement()).getRecordComponents()) {

into

    for (RecordComponentElement component : ((TypeElement)enclosing).getRecordComponents()) {

good idea,

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added regression test and updated the logic so that it breaks out of both loops instead of just the inner loop.

@mbien mbien force-pushed the goto-invalid-method-reference branch from 712cb66 to ce2ac52 Compare February 19, 2026 02:33
@mbien mbien marked this pull request as ready for review February 19, 2026 02:33
 - check if element is a record before listing components
 - removed reflective method calls and updated logic
 - added regression test
@mbien mbien force-pushed the goto-invalid-method-reference branch from ce2ac52 to 74c8117 Compare February 21, 2026 04:37
Copy link
Member Author

@mbien mbien left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rebased, planning to merge once tests are green.

Comment on lines +479 to +480
Element enclosing = el.getEnclosingElement();
if (enclosing.getKind() == ElementKind.RECORD) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw for future reference: this check basically is the fix for this issue. Without it, javac would throw a NPE a few lines later (see reproducer).

java.lang.NullPointerException: Cannot invoke "com.sun.tools.javac.code.Scope$WriteableScope.getSymbols(com.sun.tools.javac.code.Scope$LookupKind)" because the return value of "com.sun.tools.javac.code.Symbol$TypeSymbol.members()" is null
	at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:879)
	at com.sun.tools.javac.code.Symbol$TypeSymbol.getEnclosedElements(Symbol.java:818)
	at org.netbeans.modules.editor.java.GoToSupport.resolveContext(GoToSupport.java:481)

@mbien mbien added the Code cleanup Label for cleanup done on the Netbeans IDE label Feb 21, 2026
@mbien mbien merged commit 7c34673 into apache:master Feb 21, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:dev-build [ci] produce a dev-build zip artifact (7 days expiration, see link on workflow summary page) Code cleanup Label for cleanup done on the Netbeans IDE Editor Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NPE on goto invalid lambda method reference

2 participants