I have the code which below and file is on the same path but it gives error NullPointException
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
import java.util.Arrays;
import static java.io.File.separator;
public class Exception2 {
public static File file;
static Scanner scanner;
public static void main(String[] args) {
String seporator = separator;
String path = "C:"+separator+"Users"+separator+"asus"+separator+"Desktop"+separator+"1.txt";
File file = new File(path);
try {
readFile();
System.out.println("Everything is okay.");
} catch (FileNotFoundException e) {
System.out.println("Error.");
}
}
static {
try {
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
}
public static void readFile() throws FileNotFoundException {
while (scanner.hasNextLine());{
System.out.println(scanner.nextLine() );
}
scanner.close();
String line = scanner.nextLine();
String [] words = line.split(" ");
System.out.println(Arrays.toString(words));
}
}
I want to make file reader with my own exceptions The meaning of programm that it will read strings from file which places on the path which i wrote and then will give me that strings back but and i added some exceptions triggered if file will be not find out