spotbugs-gradle-plugin
spotbugs-gradle-plugin copied to clipboard
false possitive on "Possible null pointer dereference of null" on lateinit var
gradle version: 8.9 spotbugs plugin version: 5.2.5
The original failure I encountered was from a @SpringBootTest
. To simplify it for reproduction:
//Test.kt
@SpringBootTest
class Test {
@Autowired
private lateinit var bean: SomeClass
@Test
fun example() {
assertThat("a").isNotEqualTo(bean.getBeanProperty())
}
}
In the example, getBeanProperty()
might return null, but if I replace bean.getBeanProperty()
with null
, spotbugs succeeded. That makes me wonder if it was complaining about the fact that bean
object could be null. So I change to test a little to decouple it from Spring:
//Test.kt
class Test {
private lateinit var str: String
@Test
fun example() {
str.length
}
}
and it fails on the same error M C NP: Possible null pointer dereference of null in xxx.Test.example() Dereferenced at Test.kt:[line xx]
Even if I instantiate the object (but not in the same test function), it would still think the str could be null:
//Test.kt
class Test {
private lateinit var str: String
@BeforeEach
fun init() {str = "123"}
@Test
fun example() {
str.length
}
}