I have a file that I want to create, so I want to ensure that all of its parent directories are in place before it is created (or I will get an error).
File fileA = fileB.getParentFile();
Now, if fileB does in fact have a parent file, fileA will contain an actual file object. However, if fileB does not have a parent, fileA will be equal to null, and calling createNewFile() on fileA will result in a NullPointerException error. Thus, the only way to create fileA safely would be to then do the following:
if (fileA != null) {
fileA.mkdirs();
{
fileB.createNewFile();
However, the general consensus seems to be that you should never use the != null check in your code because it is poor practice. Are there exceptions to this rule, and is this one of them? Or is there a better way I could phrase this code?