protected void nullPointerProblemWithMapNOTDetectedBySonar() {
Map<String, String> someMap = new HashMap<>();
Object valueFound = someMap.get(SOME_KEY);
LOG.debug("Value is empty: "+valueFound.toString()); // NOT CORRECT: valueFound can be null
}
protected void nullPointerProblemNotDetectedBySonar() {
Map<String, String> someMap = new HashMap<>();
someMap.put(KEY, VALUE);
String valueFound = someMap.get(SOME_KEY);
if (testValue != null && valueFound.equals(testValue)) { // NOT CORRECT: possible NPE error should be reported here
LOG.debug("EQUALS");
} else {
LOG.debug("NOT EQUALS");
}
if (valueFound == null) { // NOT CORRECT: condition always false, as it was already used as non-null value
LOG.debug("Map entry is missing or null!");
}
}
protected void nullPointerProblemWithVariableNotDetectedBySonar() {
String valueFound = nullValue;
if (valueFound.equals(testValue)) { // NOT CORRECT: possible NPE error should be reported here
LOG.debug("EQUALS");
} else {
LOG.debug("NOT EQUALS");
}
if (valueFound == null) { // CORRECT?: condition always false, as it was already used as non-null value
LOG.debug("Map entry is missing or null!");
}
}
private final String nullValue = null;
private String testValue = "not found";
protected void checkNull() {
final String valueFound = nullValue;
if (valueFound.equals(testValue)) {
//do somthing
}
}
public class Example {
private String nullValue = null;
private String testValue = "not found";
protected void checkNull() {
final String valueFound = nullValue;
if (valueFound.equals(testValue)) {
//do something
}
}
protected void changeValues() {
nullValue = "";
testValue = "";
}
}
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/1110b7c4-3b0d-4f7c-bb35-b356c13861d2%40googlegroups.com.--
You received this message because you are subscribed to a topic in the Google Groups "SonarLint" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sonarlint/03C1fjq0FmI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sonarlint+unsubscribe@googlegroups.com.
protected void nullPointerProblemNotDetectedBySonar() {
Map<String, String> someMap = new HashMap<>();
someMap.put(KEY, VALUE);
String valueFound = someMap.get(SOME_KEY);
if (testValue != null && valueFound.equals(testValue)) { // NOT CORRECT: possible NPE error should be reported here
LOG.debug("EQUALS");
} else {
LOG.debug("NOT EQUALS");
}
if (valueFound == null) { // NOT CORRECT: condition always false, as it was already used as non-null value
LOG.debug("Map entry is missing or null!");
}
}
--
You received this message because you are subscribed to the Google Groups "SonarLint" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/4fca4f7f-9198-4a01-9c45-25646691ccfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Senior Developer
To unsubscribe from this group and stop receiving emails from it, send an email to sonarlint+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarlint/4fca4f7f-9198-4a01-9c45-25646691ccfd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.