Sonar qube & find bugs issues and solutions
- Unchecked/unconfirmed cast
Let’s assume there is a class RegistrationResponse, there is an instance identityResponse. Before try to cast identityResponse to RegistrationResponse you need to verify that whether identityResponse is an instance of RegistrationResponse.

2. Dead store to a variable
You create a local variable and assign it to some values. If you didn’t use the assigned value it gives the warning.
One scenario is you need to delete the variable if you are not using it anywhere, in other scenario you need to remove the value you have assigned if the assigned value is not used.
3. String is incompatible with expected argument type
If you passing String as the argument instead of any other object (This happen most of the time when you using HashMap with key as an Object because it expects an Object as a key so if you pass String it accepts it without compilation/run time error).

4. Write to static field from instance method
If multiple thread running different instances then there will be a problem. So need synchronization for the particular block. Before that think whether there could be a scenario for multi thread otherwise no need to consider this warning.

5. Redundant nullcheck of subject, which is known to be non-null.
6. Nullcheck of a variable previously dereferenced
You are checking whether a variable is null but the particular variable is already has been used.

7. Makes inefficient use of keySet iterator instead of entrySet iterator
You are retrieving all the keys first by accessing the whole map and after that if you access the map again to get the values of some or all keys you will get this warning. Because you are accessing the Map twice. There is a way to iterate through Map only once and get the values.
Map.entrySet which delivers a set of Map.Entrys each one with the key and corresponding value.So that you can iterate over the map to get map entries (Map.Entry) (couples of keys and values) and access the map only once.

8. Load of known null value
The variable referenced at this point is known to be null due to an earlier check against null. In the following example inside the if condition first you are checking whether the variable associationCleanupPeriod is null or not. If you are using or then second condition will be checked when associationCleanupPeriod is null only. So in this case if it’s null then when you check associationCleanupPeriod.trim() it will throw a null pointer exception.

9. Restrict what the loop acts on by testing each property (Js)
The for…in statement allows you to loop through the names of all of the properties of an object. The list of properties includes all those properties that were inherited through the prototype chain. This has the side effect of serving up functions when the interest is in data properties. Programs that don’t take this into account can fail.
Therefore, the body of every for…in statement should be wrapped in an if statement that filters which properties are acted upon. It can select for a particular type or range of values, or it can exclude functions, or it can exclude properties from the prototype.
Noncompliant Code Example
for (name in object) {
doSomething(name); // Noncompliant
}
Compliant Solution
for (name in object) {
if (object.hasOwnProperty(name)) {
doSomething(name);
}
}
10. Use a logger to log exception instead of print stack trace.
Doing this allows us to:
- write the log statement to different locations at once, e.g. the console and a file
- filter the log statements by severity (error, warning, info, debug etc.) and origin (normally package or class based)
- have some influence on the log format without having to change the code
