Reputation: 309
I'm having a small unexpected issue when it comes to creating new folders inside my program. Here's my method where i create my directory:
public void createDirectory() {
directory = new File("C:/Users/Lotix/Desktop/TestFolder/"
+ categoryName);
boolean isDirectoryCreated = directory.mkdir();
if (isDirectoryCreated) {
System.out.println("Created new directory in: " + directory);
} else if (directory.exists()) {
System.out.println("Category already exists!");
}
}
Here's my listener method where the method is called:
if (e.getSource() == addButton) {
System.out.println("Add Button Clicked");
botTextArea.setText("");
botTextArea.append("Adding new category...");
popUp.newCategoryPrompt();
System.out.println(popUp.getNewCategoryName());
Category cat = new Category(popUp.getNewCategoryName(), "test tag" );
cat.createDirectory();
}
And the prompt method itself:
// Prompt to enter name of the new category
public void newCategoryPrompt() {
JFrame newCatFrame = new JFrame();
newCategoryName = JOptionPane.showInputDialog(newCatFrame,
"Enter the name of new category");
}
What's the exact issue?
Program is supposed to create a folder in the already existing directory. However, when user decides to close that prompt without giving a name to a new folder, it creates a folder named "null" regardless. How do i prevent it from happening?
My temporary solution (although very sloppy) is to let user create that folder (by accident or not) and let it be since my program will print out an error and won't create it if the folder with such a name in that directory already exists.
EDIT: Same thing happens when you press cancel. EDIT2: Added the prompt method.
Upvotes: 0
Views: 584
Reputation: 1
You can try this to bypass blank and null values both:
Category cat = new Category(popUp.getNewCategoryName(), "test tag" );
if(cat.getCategoryName() != null && !"".equals(cat.getCategoryName())){
cat.createDirectory();
}
Upvotes: 0
Reputation: 660
Try
Category cat = new Category(popUp.getNewCategoryName(), "test tag" );
if(cat.getCategoryName() != null)
cat.createDirectory();
You will need to change getCategoryName() to the getter of the category name in the Category class.
Upvotes: 1