VB.Net Static variable declaration causing Null Reference Warning

The following code causes Sonar to raise a Bug warning that 'testData' is Nothing on at least one execution path.

It seems to be incorrect as it is impossible to reach the second line of code without testData being populated by the LoadTestData() method.


Public Function TestDataContains(text as String) As String
        Static testData As String = LoadTestData()
        Return testData.Contains(text)
End Function

Hi @ymwymw,

Thanks for your feedback.

Since LoadTestData() returns a string, it could theoretically return also Nothing as string is a value type.

Hi @ymwymw,

We can’t see content of the SonarCloud link.

The function returns String, and the return type of Contains is Boolean. Is that intentional?

Are there any attributes on the LoadTestData() method? Can you share them with us?

That would be the same as a standard Dim testData = LoadTestData()

But that doesnt trigger a warning.

Issue created here: Fix S2259 FP: Support VB.NET static local variables · Issue #6271 · SonarSource/sonar-dotnet · GitHub

Please let us know the answers to my questions above, it might affect the result too.

That was a mistake as I tweaked the method to make it generic.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.