avoid NullPointerException check - java

I am using SonarLint plugin (2.1.0) with Eclipse Mars (4.5.0) and I am getting : NullPointerException might be thrown as listToCheck is nullable here in this code:
if (checkListNotNull(listToCheck)) {
listToCheck.get(0); // I get here that warning
}
checkListNotNull is a method that returns true if the list that is passed is not null
Is possible to avoid this sonar warning?
Thanks

Just do that
if(listToCheck != null && !listToCheck.isEmpty()) {
listToCheck.get(0);
}
And I think the warning will disapear
(the use of another method seems to be useless in this case; the warning is just here because of Eclipse cannot understand what does your checkListNotNull do)

Related

looking for the reason for NPE in LinuxNetworkParams.getDomainName call

We are seeing the NPE in the LinuxNetworkParams.getDomainName call in oshi version 6.1.6. Although I am not able to see any reason for this. Can anyone help me when with the reasons why this can throw NPE?
Caused by: java.lang.NullPointerException
at oshi.software.os.linux.LinuxNetworkParams.getDomainName(LinuxNetworkParams.java:80) ~[oshi-core-6.1.6.jar!/:6.1.6]
at com.airwatch.common.diagnostics.DiagnosticCollector.fetchSystemConfiguration(DiagnosticCollector.java:148) ~[diagnostic-library-2.0.3.jar!/:?]
Here is the code for the method :
https://github.com/oshi/oshi/blob/oshi-parent-6.1.6/oshi-core/src/main/java/oshi/software/os/linux/LinuxNetworkParams.java#L79-L80
Looks like info.ai_canonname is null, Try adding null check for ai_canonname or use null safe way to do trim like StringUtils.trim()
public static String trim(final String str) {
return str == null ? null : str.trim();
}
As #Jishnu-Prathap said in their answer, info.ai_canonname is null.
This value normally returns the "official name of the host" in response to the getaddrinfo() function whose docs state:
If hints.ai_flags includes the AI_CANONNAME flag, then the ai_canonname field of the first of the addrinfo structures in the returned list is set to point to the official name of the host.
This may represent an error in your server configuration; fixing the "official name of the host" will prevent the NPE if you keep the same OSHI version.
However, failing to null-check this value was a bug in earlier versions of OSHI but was fixed in version 6.2.2 and upgrading will also prevent the NPE (but not give you a canonical host name, since it doesn't exist!)

VS code - SonarLint:Simplify the expression produce invalid syntax

I'm getting S1125 for a code smell:
MyVO vo;
public boolean is() {
return vo == null ? false : vo.is();
}
Remove the unnecessary boolean literal.sonarlint(java:S1125)
But when choosing Quick Fix -> SonarLint:Simplify the expression it return invalid java syntax:
return !vo == null && vo.is();
Is it Sonar Lint bug ? is my vs code settings is wrong?
Obviously it should fix to:
return vo != null && vo.is();
(Using latest VS code and sonar lint plugin)
I didn't find any issue in sonar community/Jira
Opened a question in Sonar community https://community.sonarsource.com/t/sonarlint-simplify-the-expression-wrong-syntax-on-java-s1125/62467
Which reported as SONARJAVA-4241
S1125: erroneous quick fix suggestion when negating a binary operation
and probably will be removed
It seems that covering all the cases seems tricky. We might want to simplify and simply don't suggest a quick fix if the expression is a binary operation.

ToolProvider.getSystemJavaCompiler() always returning null using jdk

I read all the posts regarding this problem and no solution works for me, I get always null.
I use JRE and put the tools.jar in the lib directory, added it to the build path but when I want to jump to declaration Eclipse wants to jump into rt.jar (?) what I totally don't understand.
Could that be the reason that I get only null? How can I configure that correctly?
What are the criteria for getSystemJavaCompiler() to return null?
Preferences screenshot
JRE is the Java Runtime Environment. It doesn't have a compiler, and therefore you're getting a null. If you use a full-fledged JDK, you'd get a non-null result.
I found a workaround for my problem. First I used the jre again. I put the tools.jar in the lib directory of the application.
ToolProvider.getSystemJavaCompiler() return null.
This is the workaround to get the JavaCompiler:
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
if (compiler == null){
try {
Class<?> javacTool = Class.forName("com.sun.tools.javac.api.JavacTool");
Method create = javacTool.getMethod("create");
compiler = (JavaCompiler) create.invoke(null);
} catch (Exception e) {
throw new AssertionError(e);
}

Parasoft code analyzer warning - 'variable is checked for null after being dereferenced'

How to avoid following error - ""user" is checked for null after being dereferenced.".
Highlighted code
OLCC olcc = saveOLCCRequest.getOLCC();
if (olcc != null) {
Thanks.
This is either a bug in the parasoft analyzer, or it has been able to determine that saveOLCCRequest.getOLCC() cannot return null.
It's not possible to say which from the two line snippet you've posted, but if getOLCC() cannot possibly return null then you can remove the warning by removing the null check.

How to use assert in android?

I want to use assert obj != null : "object cannot be null" on Android device. The assert doesn't seem to work, so I searched online and I found this local solution:
adb shell setprop debug.assert 1
it does work on my local machine.
I want to run this command using my Eclipse project(so it would be in the source control).
How can I do it?
Assert won't work in Android because most of the time a person isn't running in debug mode, but rather some optimized code. Thus, the proper solution is to manually throw an exception, with code like this:
if (obj==null) throw new AssertionError("Object cannot be null");
It should be noted that by design, Asserts are intended for debug code, and not for release time code. So this might not be the best use of throwing an Assert. But this is how you can do it still, so...
Tested on Android 4.x device, it is possible to use Java assert on Android device:
Edit /system/build.prop (for example by X-plore), add line at end of file: debug.assert=1
Reboot phone
Now your Android device is sensible to assert checks, and will throw AssertionError when assert check fails.
EDIT:
Another easy approach, enabling asserts from PC until device is restarted:
platform-tools\adb shell setprop debug.assert 1
You may for example create a .bat file (on Windows) and run it when device is attached.
Create your own assert method:
public static <T> T assertNotNull(T object) {
if (object == null)
throw new AssertionError("Object cannot be null");
return object;
}
Returning same object allows for using this in assignments for brevity.
if (somevar == null) throw new RuntimeException();
Replace RuntimeException() with an appropriate exception subtype.
Sharing my class which I use for assertions on Android, its simpler, has nice naming and very elegant because it allows you to write asserts like this:
Assert.that(obj!=null, "Object should not be null");
Here's the code for the class:
public class Assert {
public static void that(boolean condition, String message) {
if (!condition) {
throw new AssertionError(message);
}
}
}
Hope it helps!
To make assertion available in Java Virtual Machine, using -enableassertions or -ea command-line options
on Window's Command prompt,
java –ea YourApp
on Android Studio 3.0, on Tools/Edit Configurations/VM options, input -enableassertions or -ea

Categories