-2

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

housemice
  • 3
  • 1
  • Please show the exception stack trace. Please indicate which line is throwing the NullPointerException. – Nayuki Nov 23 '22 at 17:01
  • You haven't told us anything about where the exception is, or what the content of the file is. That makes it really hard to help you. The `readFile` method does seem to go to the *end* of the file, then read an extra line - I strongly suspect that `line` is null. It's not at all clear why you've got that while loop to basically exhaust the scanner *before* you try to use it. – Jon Skeet Nov 23 '22 at 17:02
  • Welcome to Stack Overflow. Please read [ask]. What **specific things** did you try already, in order to **understand** where the NullPointerException comes from? What **line of code** do you think has the exception? (Did you try to **read** the stack trace?) What **value** appears to be null, when you read the error message? Do you **understand why** it causes a problem, if that value is null? Do you believe, that some other part of the code **should be making it** not be null? If so, **why and how**? – Karl Knechtel Nov 23 '22 at 17:02
  • 5
    There are two variables called `file`. You initialize one ... and use the other one. That is the cause of the NPE. – Stephen C Nov 23 '22 at 17:03
  • 2
    Unrelated issue but you have a typo here: `while (scanner.hasNextLine());{` This semicolon is breaking this loop. Delete the semicolon. – Michael Nov 23 '22 at 17:03
  • 1
    The `static` block is going to execute **before** the `main` method ... which is where you are attempt to initialize (the wrong) `file`. Don't use a `static` block. Put that logic into a method ... – Stephen C Nov 23 '22 at 17:08

1 Answers1

2

Pretty sure your problem is here:

public class Exception2 {
    public static File file;

    static {
        try {
            scanner = new Scanner(file); // <-- NullPointerException
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

This is because all class fields are initialized to null, and the static { } block executes before main().

To solve this problem, you should move the initialization of scanner into main().

Nayuki
  • 17,911
  • 6
  • 53
  • 80