为什么我在调用此方法时得到 "null"?这是关于文件验证

Why am I getting a "null" when I call this method? This is regarding file validation

当我 运行 这个方法时,我收到 "null",为什么? 下面是代码。我没有超类,因为我的作业只需要子类,如果需要,需要其中一个子类来验证 file/create 它。这也恰好是我最难的一个。

System.out.print("Welcome to My Statistics Calculator"
        + "\nEnter the name of your text file (e.g. MyNumbers)"
        + "\nThe file extension will be added after entry."
        + "->: ");
String fn = input.nextLine();
String fileN = fn + ".txt";
String directFile = "C:\W12Assignment\" + fileN;

//calling subclass toValidate
toValidate vali = new toValidate(fileN, directFile);
System.out.println(vali.getValidDirect());
System.out.println(vali.getValidFile());

下面是我的子类:

public class toValidate extends W12Assignment {
    //constructors
    private String fileN;
    private File FN;
    private String directFile;
    private File directory;

    public toValidate(String fiNa, String dire) {
        fileN = fiNa;
        directFile = dire;

        //calling methods
        setValidFile(fileN);
        setValidDirect(directFile);

    }

    public File getValidFile() {
        return FN;
    }

    public File getValidDirect() {
        return directory;
    }

    private File setValidFile(String fileN) throws IOException {
        File FN = new File(fileN);
        if (!FN.exists()) {
            System.out.println("The file does not exist.\nCreating file...");
            FN.createNewFile();
            System.out.printf("The file now exists and is located here:\n%s\n",
                    FN.getAbsolutePath());
        } else {
            System.out.printf("The file already exists and is located here:\n%s\n",
                    FN.getAbsolutePath());
        }
        return FN;
    }

    private File setValidDirect(String directFile) {
        File directory = new File(directFile);
        if (!directory.exists()) {
            directory.mkdirs();
            System.out.println("The directory does not exist.\nCreating directory...");
        } else {
            System.out.printf("The directory does exist at this location: \n%s\n",
                    directory.getAbsolutePath());
        }
        return directory;
    }
}

您似乎用方法变量隐藏了 class 变量。尝试改变

File FN = new File(fileN);

 FN = new File(fileN);

你的方法 setValidFile()