apache / apache/netbeans

Refactor Rename AssrtionError: SWITCH_EXPRESSION

Open
#9,376 1 comment 0 reactions 0 assignees View on GitHub
Java kind:bug needs:triage
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 30 latest release candidate

### What happened

Exception during refactor rename.

### Language / Project Type / NetBeans Component

Refactor rename in java source

### How to reproduce

1. Position the caret on `comp` in `for (Comp comp : some_list)`
2. Select `Menu > Refactor > Rename ...`
3. change comp to xxx (or anythgin)
4. \
Observe exception

```java
package com.raelity.play.refactor_switch;

import java.util.List;

import javax.swing.JLabel;

public class Refactor_switch {
enum CompDim { H1 }
private record Comp(JLabel label, CompDim dim){};
private List some_list = null;

public static void main(String[] args) {
}

public void buildGui_dim()
{
for (Comp comp : some_list) { // REFACTOR RENAME comp GETS EXCEPTION
comp.label.setPreferredSize( switch (comp.dim) {
default -> null;
});
}
}

}
```
The following fails with a NullPointerException
```java
package com.raelity.play.refactor_switch;

public class Refactor_switch {
private record Comp(int i){};

public void buildGui_dim()
{
Comp comp = new Comp(1);
Object a = switch (comp.i) {
default -> null;
};
}

}
```
```
INFO [org.netbeans.modules.refactoring.spi.impl.ParametersPanel]: org.netbeans.modules.refactoring.api.AbstractRefactoring$ProgressL@5f24ddec called start multiple times
INFO [org.netbeans.modules.refactoring.spi.impl.ParametersPanel]: org.netbeans.modules.refactoring.api.AbstractRefactoring$ProgressL@5f24ddec called start multiple times
INFO [org.netbeans.api.java.source.TreePathHandle]: Cannot resolve: TreePathHandle[delegate:TreeDelegate[kind:VARIABLE, enclosingElement:ElementHandle[kind=METHOD; sigs=com.raelity.play.refactor_switch.Refactor_switch buildGui_dim ()V ], file:/home/err/NetBeansProjects/refactor_switch/src/main/java/com/raelity/play/refactor_switch/Refactor_switch.java@1d1025e:330832a]]
SEVERE [org.openide.util.Exceptions]
java.lang.NullPointerException: Cannot invoke "javax.lang.model.element.VariableElement.getEnclosingElement()" because "var" is null
at org.netbeans.modules.j2ee.jpa.refactoring.rename.RelationshipMappingRename$1.run(RelationshipMappingRename.java:126)
```
### Did this work correctly in an earlier version?

No / Don't know

### Operating System

Linux harmony 6.17.9-76061709-generic #202511241048~1764704751~22.04~b24b425 SMP PREEMPT_DYNAMIC Tue D x86_64 x86_64 x86_64 GNU/Linux

### JDK

openjdk 26 2026-03-17 OpenJDK Runtime Environment (build 26+35-2893) OpenJDK 64-Bit Server VM (build 26+35-2893, mixed mode, sharing)

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

Happens every time.

### Are you willing to submit a pull request?

No

Contributor guide

Open the contributing guide

Research direction

Reproduce the rename failure using the Java examples in the issue, then inspect org.netbeans.modules.j2ee.jpa.refactoring.rename.RelationshipMappingRename.java around line 126 and the TreePathHandle resolution message. Done means renaming the loop variable or local variable in the switch-expression examples completes without the reported NullPointerException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.