-3

I have the following codes but fails when using findbugs.

    protected void writeStream(final InputStream inputStream, final Path destinationFile) throws IOException {
    final Path parentDirectory = destinationFile.getParent();
    Path tempFile = null;
    try {
        // Create the temporary dir for temporary download
        Files.createDirectories(getTempDownloadPath());
        Files.createDirectories(parentDirectory);
        // Create the temporary file in the temporary dir
        tempFile = Files.createTempFile(getTempDownloadPath(), destinationFile.getFileName().toString(), ".tmp");
        long t1 = System.currentTimeMillis();
        Files.copy(inputStream, tempFile, REPLACE_EXISTING);
        long t2 = System.currentTimeMillis();
        Files.move(tempFile, destinationFile, ATOMIC_MOVE, REPLACE_EXISTING);
        long t3 = System.currentTimeMillis();
    } catch (final IOException e) {
        log.error("Failed to write file.", e);
        throw e;
    } finally {
        IOUtils.closeQuietly(inputStream);
        try {
            if (tempFile != null) {
                Files.deleteIfExists(tempFile);
            }
        } catch (IOException e) {
            log.warn("Failed to delete file: " + tempFile, e);
        }
    }
}

It is complaining that

tempFile = Files.createTempFile(getTempDownloadPath(), destinationFile.getFileName().toString(), ".tmp");

Possible null pointer dereference due to return value of called method

Which part is wrong and how can I fix this?

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
chrisTina
  • 2,298
  • 9
  • 40
  • 74

1 Answers1

0

Files.createTempFile does not return a Path object, but a string.

Curt
  • 5,518
  • 1
  • 21
  • 35