#fileDelete method not working

12 messages · Page 1 of 1 (latest)

junior hatchBOT
#

This post has been reserved for your question.

Hey @hollow owl! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

hollow owl
#

im always getting this error

#

despite the fact that the file exists? and the user entered in the correct name

#

as u can see all the debugging statements print out information that makes sense

#

meaning the function shld work

#

i have added all these debugging statements yet i still cannot find the issue?

#

can someone help pls

lost forumBOT
#

@FXML ```java
void fileDelete(ActionEvent event) {
String baseDirectory = "/home/ntu-user/App";
// String enteredFileName = deleteFileText.getText();
String enteredFileName = deleteFileText.getText().trim(); // Trim to remove leading/trailing spaces

    for (char c : enteredFileName.toCharArray()) { // checking for hidden charactres.
        System.out.println("Character: " + c + " ASCII code: " + (int) c);
    }

    System.out.println("ENTERED FILE NAME: " + enteredFileName); // Debug statement

    if (enteredFileName.isEmpty()) {
        JavaFXUtility.error("Cannot delete file - Empty fields", "Please enter a file name into the text box");
        return;
    }

    String filePath = baseDirectory + File.separator + enteredFileName;
    File fileToDelete = new File(filePath);

    System.out.println("FILE PATHHHHH: " + filePath);

    if (!fileToDelete.exists()) {
        // Print debugging information to help diagnose the issue
        System.out.println("ENTERED FILE NAMEEEEEEEEEE: " + enteredFileName);
        System.out.println("FILES IN DBBB in the database:");

        ObservableList<FileMetadata> data = metadataTableView.getItems();
        for (FileMetadata metadata : data) {
            System.out.println(metadata.getFileName());
        }

        JavaFXUtility.error("Cannot delete a nonexistent file.", "Please enter a valid file name.");
        return;
    } ```

    boolean confirmed = JavaFXUtility.confirmation(
            "Are you sure you want to delete the file '" + enteredFileName + "'?",
            "This action is irreversible!"
    );

This message has been formatted automatically. You can disable this using /preferences.

#
            boolean fileDeleted = fileToDelete.delete();

            if (fileDeleted) {
                // Get the current items from the table
                ObservableList<FileMetadata> data = metadataTableView.getItems();

                // Find the FileMetadata object to remove
                FileMetadata fileToRemove = null;

                for (FileMetadata metadata : data) {
                    if (metadata.getFileName().equals(enteredFileName)) {
                        fileToRemove = metadata;
                        break;
                    }
                }

                if (fileToRemove != null) {
                    // Remove the item directly from the table view
                    data.remove(fileToRemove);

                    JavaFXUtility.success("File successfully deleted!", "You can now no longer see it on the table!");
                }
            }

            // Update the table view with the latest data
            initialiseFileMetadataTable();
        }
        deleteFileText.clear();
    } ```

This message has been formatted automatically. You can disable this using /preferences.

hollow owl
analog yacht
#

So what? The error happens when the file is not on disk. What's troubling you?