apache / apache/netbeans

Replace if chain with switch can throw NPEs

Open
#8,266 0 comments 1 reaction 0 assignees View on GitHub
hints Java kind:bug
Dominant language
Java
Stars
3.1k
Forks
935
Avg merge
2d 3h
Merged PRs (30d)
17

Description

### Apache NetBeans version

Apache NetBeans 24

### What happened

simplified snippet taken from [NB repo](https://github.com/apache/netbeans/blob/778fd731151fc431f02f2aa8cb5818cb718cb8ce/java/maven/src/org/netbeans/modules/maven/execute/CommandLineOutputHandler.java#L771-L785):

```java
private void checkProgress(ExecutionEventObject eeo) {
if (ExecutionEvent.Type.ProjectDiscoveryStarted.equals(eeo.type)) {
} else if (ExecutionEvent.Type.SessionStarted.equals(eeo.type)) {
} else if (ExecutionEvent.Type.ProjectStarted.equals(eeo.type)) {
}
}
```
run hint -> convert to switch statement

results:
```code
java.lang.NullPointerException: Cannot invoke "org.netbeans.spi.java.hints.HintContext.getInfo()" because "this.ctx" is null
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitPrimitiveWrapperMethods(NPECheck.java:1076)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitMethodInvocation(NPECheck.java:1042)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitMethodInvocation(NPECheck.java:575)
at com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1877)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at org.netbeans.api.java.source.support.CancellableTreeScanner.scan(CancellableTreeScanner.java:69)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.scan(NPECheck.java:677)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitBinary(NPECheck.java:898)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitBinary(NPECheck.java:575)
at com.sun.tools.javac.tree.JCTree$JCBinary.accept(JCTree.java:2231)
at com.sun.source.util.TreeScanner.scan(TreeScanner.java:91)
at org.netbeans.api.java.source.support.CancellableTreeScanner.scan(CancellableTreeScanner.java:69)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.scan(NPECheck.java:677)
at org.netbeans.modules.java.hints.bugs.NPECheck$VisitorImpl.visitNewClass(NPECheck.java:1008)
```

null seems to be a legal value for `ctx` - judging from the null checks in other places of the class.

### Language / Project Type / NetBeans Component

_No response_

### How to reproduce

see above

### Did this work correctly in an earlier version?

No / Don't know

### Operating System

linux

### JDK

23

### Apache NetBeans packaging

Apache NetBeans binary zip

### Anything else

_No response_

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

No - not right now

Contributor guide

Open the contributing guide

Research direction

Start with the NPECheck visitor methods named in the stack trace, especially visitPrimitiveWrapperMethods and visitMethodInvocation, and reproduce the issue using the checkProgress snippet from CommandLineOutputHandler.java. Done means converting the if chain to a switch no longer causes a NullPointerException while the hint continues to report applicable issues.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.