Skip to content

[Bug]: NPE on getEndColumn #6702

@strRM

Description

@strRM

Describe the bug

Even though the source position of a CtCompilationUnit is valid, calling getEndColumn() throws an exception.

I'm running this in a JUnit test with a VirtualFile. If the file contains newlines I get the error, otherwise I don't.

Note that I extracted this from another unit test, so the bottom of the stacktrace does not match what you would get.

FWIW, the end position is completely bogus. I get 2147483646 for an end position.

Source code you are trying to analyze/transform

class Source {
 // newline is essential; seems to work fine without newline
}

Source code for your Spoon processing

void test(String sourceCode) {
		Launcher launcher = new Launcher();
		// Load from test resources
		launcher.addInputResource(new VirtualFile(sourceCode, "x/y/z/Source.java"));
		launcher.getEnvironment().setNoClasspath(true);
		launcher.getEnvironment().setComplianceLevel(17);
		launcher.buildModel();

		assert(1== launcher.getFactory().CompilationUnit().getMap().size());
		CtCompilationUnit ct = launcher.getFactory().CompilationUnit().getMap().values().iterator().next();
		assert(ct.getPosition().isValidPosition());
		ct.getPosition().getEndColumn();
}

Actual output

java.lang.NullPointerException: Cannot invoke "String.charAt(int)" because "source" is null
	at spoon.support.reflect.cu.position.SourcePositionImpl.searchColumnNumber(SourcePositionImpl.java:87)
	at spoon.support.reflect.cu.position.SourcePositionImpl.getEndColumn(SourcePositionImpl.java:132)
	at us.str.axle.spoon.callbacks.Standard_JUnit.execCallback(Standard_JUnit.java:105)
	at us.str.axle.spoon.callbacks.Standard_JUnit.testSimple(Standard_JUnit.java:130)

Expected output

Spoon Version

11.3.0

JVM Version

Java17

What operating system are you using?

Alma Linux 9

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions