[Bug]: NPE on getEndColumn
- Dominant language
- Java
- Stars
- 2k
- Forks
- 392
- Avg merge
- 11h 24m
- Merged PRs (30d)
- 36
Description
### 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
```Java
class Source {
// newline is essential; seems to work fine without newline
}
```
### Source code for your Spoon processing
```Java
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
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
```Java
```
### Spoon Version
11.3.0
### JVM Version
Java17
### What operating system are you using?
Alma Linux 9
Contributor guide
Research direction
Start with spoon.support.reflect.cu.position.SourcePositionImpl.java, especially searchColumnNumber and getEndColumn, and reproduce the issue with the provided Launcher, VirtualFile, and newline-containing Java source. Done means the valid CtCompilationUnit position no longer throws a NullPointerException when getEndColumn() is called and the reported end position is no longer the bogus value.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100