I have a program that searches through various text files. My main GUI class has no problems getting the proper value from the properties. However, I have a “starter” GUI that opens if the filepath is either null or doesn’t contain the files its looking for and prompts the user to select the proper folder.
My code is as follows
public void mainMethod() throws FileNotFoundException, IOException {
// Creating an instance of this class so we can call the various methods
FileNotFoundGUI FNFGUI = new FileNotFoundGUI();
// Finding the current path the program is running on then backing up and navigating
// to the properties file to set the default fileLocation to the spot chosen by the
// user after they finished navigating the files to where they stored Tracking program
String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
rootPath = (rootPath.substring(1, rootPath.length() - 5) + "\src\DataParsing\config.Properties").replace("/","\");
Properties prop = new Properties();
// System.out.println(rootPath);
//prop.setProperty("FileLocation", "C:\Users\nawcm\Desktop\Trickery\Tracking Program\");
System.out.println(prop.getProperty("FileLocation"));
if (foundFile == false) {
if (prop.getProperty("FileLocation") == null) {
System.out.println("Null Property");
FNFGUI.NoFile();
} else {
File testFile = new File(prop.getProperty("FileLocation") + "Information Folders\FoundAircraft.txt");
try (Scanner fileLook = new Scanner(testFile)) {
foundFile = true;
} catch (FileNotFoundException e) {
System.out.println("File Not Found: " + e);
FNFGUI.NoFile();
}
}
if (foundFile == true) {
stGui();
}
}
}
And my properties follow is as follows
FileLocation=C:\Users\nawcm\Desktop\Trickery\Tracking Program\
Like I said, when I get the property it constantly returns null, but if I set the property (commented out line) to whats in the properties file, it works just fine. I have pretty much this exact snippit of code in my main GUI class with no problems.
The reason this is in a custom class called mainMethod is because another question similar to mine said to not use a static class to avoid issues. Otherwise, I’m not sure what to do here.