FORWARD_NULL after derefencing null?
-
27-05-2021 - |
Domanda
I have this line of code:
this.Path = pathLookUpLocation.GetValue(RegLookupKey, null).ToString();
When I run static analysis tool (Coverity) on my code I get a FORWARD_NULL here, saying that I am dereferencing null here. I am having trouble understanding what that means and how I would go about fixing it?
this.Path is a string, pathLookUpLocation is a RegistryKey, RegLookupKey is a string.
Soluzione
I suppose pathLookUpLocation
is of type RegistryKey
.
The reason for this message is that your code will throw a NullReferenceException
if the value with the key specified by RegLookupKey
is not found. This happens, because you pass null
as the second parameter to GetValue
. The second parameter is the default value that is returned if the key can't be found.
Fix it by changing it to string.Empty
:
this.Path = pathLookUpLocation.GetValue(RegLookupKey, string.Empty).ToString();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow