Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

false possitive on "Possible null pointer dereference of null" on lateinit var #1225

Open
yangzii0920 opened this issue Aug 7, 2024 · 0 comments

Comments

@yangzii0920
Copy link

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
    } 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant