title: How to avoid null comparison with file.getParentFile()?
body: <p>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).</p>
  
  <pre><code>File fileA = fileB.getParentFile();
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>if (fileA != null) {
      fileA.mkdirs();
  {
  fileB.createNewFile();
  </code></pre>
  
  <p>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?</p>