Skip to content

Replace if chain with switch can throw NPEs #8266

Open
@mbien

Description

@mbien

Apache NetBeans version

Apache NetBeans 24

What happened

simplified snippet taken from NB repo:

    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:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Java[ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form)hintskind:bugBug report or fix

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions