title: Why does checking whether a file exists in hadoop cause a NullPointerException?
body: <p>I'm trying to create or open a file to store some output in HDFS, but I'm getting a NullPointerException when I call the <code>exists</code> method in the second to last line of the code snippet below:</p>
  
  <pre><code>DistributedFileSystem dfs = new DistributedFileSystem();
  Path path = new Path("/user/hadoop-user/bar.txt");
  if (!dfs.exists(path)) dfs.createNewFile(path);
  FSDataOutputStream dos = dfs.create(path);
  </code></pre>
  
  <p>Here is the stack trace:</p>
  
  <pre><code>java.lang.NullPointerException
          at org.apache.hadoop.dfs.DistributedFileSystem.getFileStatus(DistributedFileSystem.java:390)
          at org.apache.hadoop.fs.FileSystem.exists(FileSystem.java:667)
          at ClickViewSessions$ClickViewSessionsMapper.map(ClickViewSessions.java:80)
          at ClickViewSessions$ClickViewSessionsMapper.map(ClickViewSessions.java:65)
          at org.apache.hadoop.mapred.MapRunner.run(MapRunner.java:47)
          at org.apache.hadoop.mapred.MapTask.run(MapTask.java:227)
          at org.apache.hadoop.mapred.TaskTracker$Child.main(TaskTracker.java:2209)
  </code></pre>
  
  <p>What could the problem be?</p>