Problem z Scannerem

Tadzio | 2021-02-17 10:25:55 UTC | #1

Mam taki problem.
Gdy wpisze:
Scanner read = new Scanner(new File(“/home/tadzio/Reg/config.txt”));
to jest ok
a jesli zrobie tak:
String path = “/home/tadzio/Reg/config.txt”;
Scanner read = new Scanner(new File(path));
to wywala:
Exception in thread “main” java.lang.NullPointerException
at java.io.File.(File.java:277)
at main.Main.menu(Main.java:47)
at main.Main.main(Main.java:18)
w linijce 47 jest: Scanner read = new Scanner(new File(path));


Nieznajomy11 | 2017-05-26 19:34:26 UTC | #2

[code]
String path = “/home/tadzio/Reg/config.txt”;
try {
Scanner read = new Scanner(new File(path));
} catch (FileNotFoundException e) {
System.out.println(“File not found!”);
e.printStackTrace();
}
[/code]
ewentualnie można też próbować tak:
[code]
try {
File file = new File(“/home/tadzio/Reg/config.txt”);
Scanner read = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println(“File not found!”);
e.printStackTrace();
}
[/code]

Wydaje mi się, że tak powinno działać :v


Tadzio | 2017-05-26 19:34:22 UTC | #3

Thx :D


system | 2021-02-17 10:26:09 UTC | #4

Ten temat został automatycznie zamknięty 32 dni po ostatnim wpisie. Tworzenie nowych odpowiedzi nie jest już możliwe.