We are doing static code analysis using SonarQube. We are facing following Bug found by SonarQube
NullPointerException might be thrown as 'evt' is nullable here
in following code
try {
//business logic
}(Exception e){
throw new MyException("Found issue for event " + evt.getDeatil());
}
So to remove this bug we have introduced small inline code for null check
try {
//business logic
}(Exception e){
throw new MyException("Found issue for event " + evt != null ?
evt.getDeatil() : null);
}
Still SonarQube showing same violation bug after code change.
Could anyone please help us to find what we are missing here?
You have an error in line:
throw new MyException("Found issue for event " + evt != null ? evt.getDeatil() : null);
The problem is operators precedence . +
is evaluated before ?:
so your code is equal to:
String message = "Found issue for event " + evt;
throw new MyException(message != null ? evt.getDeatil() : null);
NullPointerException still can occur. Additional you've introduced a new problem - expression:
message != null
always evaluates to true
.
To fix it you have to add curly brackets:
throw new MyException("Found issue for event " + (evt != null ? evt.getDeatil() : null));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.