Java – “Result of method is ignored”- what does this imply

androidjava

For some methods I get the warning. This is what it says when expanded.
The following code (mkDirs()) gives the warning

if (!myDir.exists()) {
     myDir.mkdirs();
}

Reports any calls to specific methods where the result of that call is ignored. Both methods specified in the inspection's settings and methods annotated with org.jetbrains.annotations.Contract(pure=true) are checked. For many methods, ignoring the result is perfectly legitimate, but for some methods it is almost certainly an error. Examples of methods where ignoring the result of a call is likely to be an error include java.io.inputStream.read(), which returns the number of bytes actually read, any method on java.lang.String or java.math.BigInteger, as all of those methods are side-effect free and thus pointless if ignored.

What does it mean? How to to avoid it? How should it be addressed?

Best Answer

that the (compilation) warning message really says this:

Result of File.mkdir() is ignored

something like that. It is telling you that you are ignoring the result of the mkdir() call that tells you whether or not a directory was created.

One way to avoid the warning would be to test the result and act appropriately. Another would be to simply assign the result to a temporary variable, ignore it, and (potentially) crash later because the directory wasn't created when it should have been.

Feel free to modify the code if there is any other mistake.

Since you asked ... it is BAD STYLE to use Hungarian notation for Java variable names. Java is a strongly typed language where all variables have a clear declared types. You should not need the mental crutches of some ghastly identifier convention to tell you what a variable's type is intended to be.

you can handled in these ways

1)

boolean isDirectoryCreated= path.mkdirs();

and ignore 'isDirectoryCreated'

2) (Recommended)

boolean isDirectoryCreated= path.exists();
 if (!isDirectoryCreated) {
     isDirectoryCreated= path.mkdirs();
 }
 if(isDirectoryCreated) {
    // do something
 }