简体   繁体   中英

Exception in thread “main” when calling methods from switch in menu class

My problem is: When I'm trying to call method newFile.createYear(); from switch in menu terminal prints error:

Exception in thread "main" java.lang.NullPointerException
at com.register.file.CreateFile.showYears(CreateFile.java:140)
at com.register.file.CreateFile.createYear(CreateFile.java:30)
at com.register.main.Menu.registers(Menu.java:30)
at com.register.main.Menu.menu(Menu.java:132)
at com.register.main.Main.main(Main.java:14)

Problem does not occur when I'm running program inside eclipse. I'm running program by typing java com.register.main.Main in terminal.

Additionally when I'm trying to go back to last menu, after calling a method, (even in Eclipse):

The part of code from switch:

    switch (selection) {
            case 1:
//method that clearing terminal (I can't find anything better :( )
                newClear.clearConsole();
//method that work only in eclipse, not terminal
                newFile.createYear();
//name of current menu (back option) - doesn't work
                registers();

                break;

> Exception in thread "main" java.util.NoSuchElementException   at
> java.util.Scanner.throwFor(Unknown Source)    at
> java.util.Scanner.next(Unknown Source)    at
> java.util.Scanner.nextInt(Unknown Source)     at
> java.util.Scanner.nextInt(Unknown Source)     at
> com.register.main.Menu.registers(Menu.java:24)    at
> com.register.main.Menu.registers(Menu.java:31)    at
> com.register.main.Menu.menu(Menu.java:134)    at
> com.register.main.Main.main(Main.java:14)

https://github.com/JakubKacperski/Register

Add a check if the directory "Registers" exists in your class CreateFile in method showYears() :

    File yearsDirs = new File("Registers"); 

    if (!yearsDirs.exists()){ //Create "Registers" when it does not exists.
        yearsDirs.mkdir();
    }

    File[] dirs = yearsDirs.listFiles();

    for (File dir : dirs) {
        if (dir.isDirectory()) {
            System.out.print("Registers ");
        } else {
            System.out.print("Register ");
        }
        try {
            System.out.println(dir.getName());
        } catch (Exception e) {
            System.out.println("Error.");
        }
    }

Without the check, you get a NullPointerException when calling .listFiles() 'cause the directory "Registers" does not exist. In Eclipse, you probably have a directory "Registers" under your project Register. When called from the terminal, you've probably not.


Per comment of OP: you can write a loop like:

 public class Main { public static void main(String[] args) { Menu newMenu = new Menu(); boolean continuePlaying = newMenu.menu(); while (continuePlaying) { continuePlaying = newMenu.menu(); } } } 

And change your method menu() s return type to boolean instead of void .


Also, you are using multiple Scanner s. I suggest using only one. When you close a Scanner , it looks like it also closes the other ones. In your method createYear() in class CreateFile you call reader.close() at the end. When I remove that line, the above loop works perfectly. When I let it as it is, I get a NoSuchElementException like you got.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM