/** *Method called by the menu to gather, transform, and verify user *input to be used in the findFile() recursive method *If the input is valid, the findFile() method is called with the parameter *If the input is invalid, action is taken to ensure valid input */ public static void callFindFilePath() { //lets user know the method has been called and creates Scanner for input System.out.println("callFindFilePath() has been called" + "\n"); Scanner input = new Scanner(System.in); //declares flag variable for when user does not want to try again boolean tryAgain = true; String findFile = ""; String startDirectory = ""; //while loop that continues until user exits the method while(tryAgain) { try { //prompts user to enter a directory to search for a file in //and sets input to variable answer, also creates File object set to user input System.out.print("Please Enter a Start Path> "); startDirectory = input.nextLine(); File directory = new File(startDirectory); //checks to see if user input is a valid directory or no if(directory.isDirectory()) { //prompts user to enter a file to find paths for System.out.print("Please Enter a File to Find the Paths of> "); findFile = input.nextLine(); File searchFile = new File(findFile); // System.out.println("findFile() has been called"); findFile(searchFile, directory); System.out.println(""); tryAgain = mainMenu(); } //throws argument if user input is not a valid directory else throw new IllegalArgumentException(); } //catches user input not being a valid directory and prompts user to return to main menu or not catch(IllegalArgumentException iae) { System.out.println(startDirectory + " Is Not a Valid Starting Directory, Please Try Again" + "\n"); tryAgain = mainMenu(); } } } /** *The basic findFile algorithm *This method does not check the parameters and it's only called by *the callFindFilePath() method * *@param targetFileName the file to have its absolute path searched for and printed *@param startPath the starting directory to be searched for targetFileName */ public static void findFile(File targetFileName, File startPath) { //creates File array and populates it with all files in startPath File [] fileArray = startPath.listFiles(); //iterates through fileArray to find matching file to targetfileName for(int i = 0; i < fileArray.length; i++) { // if(fileArray[i].isFile()) if(fileArray[i].getName().matches(targetFileName.getName())) System.out.println(fileArray[i].getAbsolutePath() + "\n"); // else findFile(targetFileName,fileArray[i]); } } I keep getting this error: -----------Options Menu----------- A) Summation B) Isabel's Technique C) Find File Path Q) Quit Enter Choice> c callFindFilePath() has been called Please Enter a Start Path> rdty rdty Is Not a Valid Starting Directory, Please Try Again Would You Like to Return to the Main Menu (Y/N)> C;\\ Please Enter Either Y or N Would You Like to Return to the Main Menu (Y/N)> n Please Enter a Start Path> C:\\Windows Please Enter a File to Find the Paths of> BCD findFile() has been called Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "fileArray" is null at Recursion.findFile(Recursion.java:303) at Recursion.findFile(Recursion.java:312) at Recursion.callFindFilePath(Recursion.java:270) at Client.main(Client.java:56) C:\Users\Noahh\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:111: The following error occurred while executing this line: C:\Users\Noahh\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:68: Java returned: 1 BUILD FAILED (total time: 19 seconds) Please Help
/**
*Method called by the menu to gather, transform, and verify user
*input to be used in the findFile() recursive method
*If the input is valid, the findFile() method is called with the parameter
*If the input is invalid, action is taken to ensure valid input
*/
public static void callFindFilePath()
{
//lets user know the method has been called and creates Scanner for input
System.out.println("callFindFilePath() has been called" + "\n");
Scanner input = new Scanner(System.in);
//declares flag variable for when user does not want to try again
boolean tryAgain = true;
String findFile = "";
String startDirectory = "";
//while loop that continues until user exits the method
while(tryAgain)
{
try
{
//prompts user to enter a directory to search for a file in
//and sets input to variable answer, also creates File object set to user input
System.out.print("Please Enter a Start Path> ");
startDirectory = input.nextLine();
File directory = new File(startDirectory);
//checks to see if user input is a valid directory or no
if(directory.isDirectory())
{
//prompts user to enter a file to find paths for
System.out.print("Please Enter a File to Find the Paths of> ");
findFile = input.nextLine();
File searchFile = new File(findFile);
//
System.out.println("findFile() has been called");
findFile(searchFile, directory);
System.out.println("");
tryAgain = mainMenu();
}
//throws argument if user input is not a valid directory
else
throw new IllegalArgumentException();
}
//catches user input not being a valid directory and prompts user to return to main menu or not
catch(IllegalArgumentException iae)
{
System.out.println(startDirectory + " Is Not a Valid Starting Directory, Please Try Again" + "\n");
tryAgain = mainMenu();
}
}
}
/**
*The basic findFile
*This method does not check the parameters and it's only called by
*the callFindFilePath() method
*
*@param targetFileName the file to have its absolute path searched for and printed
*@param startPath the starting directory to be searched for targetFileName
*/
public static void findFile(File targetFileName, File startPath)
{
//creates File array and populates it with all files in startPath
File [] fileArray = startPath.listFiles();
//iterates through fileArray to find matching file to targetfileName
for(int i = 0; i < fileArray.length; i++)
{
//
if(fileArray[i].isFile())
if(fileArray[i].getName().matches(targetFileName.getName()))
System.out.println(fileArray[i].getAbsolutePath() + "\n");
//
else
findFile(targetFileName,fileArray[i]);
}
}
I keep getting this error:
-----------Options Menu-----------
A) Summation
B) Isabel's Technique
C) Find File Path
Q) Quit
Enter Choice> c
callFindFilePath() has been called
Please Enter a Start Path> rdty
rdty Is Not a Valid Starting Directory, Please Try Again
Would You Like to Return to the Main Menu (Y/N)> C;\\
Please Enter Either Y or N
Would You Like to Return to the Main Menu (Y/N)> n
Please Enter a Start Path> C:\\Windows
Please Enter a File to Find the Paths of> BCD
findFile() has been called
Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "fileArray" is null
at Recursion.findFile(Recursion.java:303)
at Recursion.findFile(Recursion.java:312)
at Recursion.callFindFilePath(Recursion.java:270)
at Client.main(Client.java:56)
C:\Users\Noahh\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\Noahh\AppData\Local\NetBeans\Cache\14\executor-snippets\run.xml:68: Java returned: 1
BUILD FAILED (total time: 19 seconds)
Please Help
Step by step
Solved in 2 steps