-2

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?

Sam Hanley
  • 4,707
  • 7
  • 35
  • 63
Denis
  • 11
  • 4

2 Answers2

0

Replace Scanner scanner with Scanner scanner = new Scanner(System.in)

Atri
  • 5,511
  • 5
  • 30
  • 40
  • Thank you Atri, totally missed to enter full scanner class .... so simple but effective :) – Denis Jan 29 '16 at 18:51
0

Try to add Scanner:

  Scanner scanner = new Scanner(System.in);
  inputUser = scanner.next().toUpperCase();
Abdelhak
  • 8,299
  • 4
  • 22
  • 36