Skip to content
Advertisement

Result of ‘File.createNewFile()’ is ignored

I’m not sure why this is but in my IDE, for file.createNewFile(); I’m getting Result of 'File.createNewFile()' is ignored for it which I’ve no idea what I should be doing

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;

public class SQL {

    public Connection connection;

    public SQL(File file) {
        try {
            file.createNewFile(); // Result of 'File.createNewFile()' is ignored
            Class.forName("org.sqlite.JDBC");
            connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
        } catch(Exception exception) {
            exception.printStackTrace();
        }
    }
}

Advertisement

Answer

Static Code Inspection in your IDE for createNewFile might be complaining this because createNewFile returns a boolean value. This is in place so that you have a way to determine if the creation of file is successful or not and act accordingly. Right now the boolean result is not assigned to any variable, so it’s ignored.

Ideally you should check the result of file.createNewFile() and validate whether file actually got created before moving on to setting up connection.

// Create File
boolean fileCreated = file.createNewFile()
// Validate that file actually got created
if (!fileCreated) {
    throw new IOException("Unable to create file at specified path. It already exists");
}

// Proceed to setting up connection
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement