I am trying to create a folder by having a user typing the name of the folder:
Scanner scanner;
String inputUser;
public void createDir(String input){
System.out.print("Please enter name of Folder: ");
this.inputUser = this.scanner.next().toUpperCase();
File makeDir = new File("C:\\" + this.inputUser);
try{
if(!makeDir.exists()){
makeDir.mkdir();
System.out.println("You have created directory " + makeDir.getName());
}else{
System.out.println("Directory with name " + makeDir.getName() + " already exists.");
}
}catch(Exception e){
System.out.println("Error while creating directory name " + makeDir.getName());
}
}
but every time when I run it I am getting the following error message:
Exception in thread "main" java.lang.NullPointerException Please enter name of Folder: at filemanagerapp.FileManagerSystem.createDir(FileManagerSystem.java:68) at filemanagerapp.FileManagerApp.main(FileManagerApp.java:42)
Java Result: 1
I have noticed that the line that is causing the problem is this.inputUser
as when I remove it and try hard coding the name of folder to create, it creates normally with no problem, but I want user to enter name of folder if possible.
Can anyone tell me why this error is happening?