Reputation: 83
FileExaminer(String filename) {
System.out.println(filename);
java.io.File file = new java.io.File(filename);
System.out.println(file);
Scanner input = null;
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("fiel not found" + file);
}
System.out.println(input);
System.out.println(input.hasNext());
while(input.hasNextLine()) {
lines.add(input.nextLine());
}
calcluateCharCountAlpha();
}
I'm having a strange problem. this is throwing a null pointer exception (also file not found exception). I assumed that for some reason the line setting input to equal file isn't working because of the input = null
line. But if I remove the = null part
ex:
Scanner input;
instead of
Scanner input = null;
it says that I need to "initialize" the variable. if I delete the line entirely and change
try {
input = new Scanner(file);
} catch
to
try {
Scanner input = new Scanner(file);
} catch
then the input can't be resolved
and if i delete the above and try to just do
Scanner input = new Scanner(file);
where
Scanner input = null;
is. (not in the try/catch block) it says it needs a try/catch block.
so as far as I can tell. I can't declare input in the try/catch or its inaccessible outside of the try/catch.
If I declare it outside and assign it a value inside the try/catch without assigning it a value outside the try/catch then its not initialized
If I declare it outside and assign it a value outside and a give it a value inside the outside will supersede the value given inside the try/catch
If I only declare it and assign its value outside of a try/catch it says that it has to be in a try/catch.
so how is this supposed to work?(this is starting to get really frustrating)
Upvotes: 2
Views: 585
Reputation: 1596
Surround all the file operations part of your method with try catch to catch the error, and use Exception
in catch block to catch all the exceptions.
Try this:
FileExaminer(String filename) {
try {
System.out.println(filename);
java.io.File file = new java.io.File(filename);
System.out.println(file);
Scanner input = null;
input = new Scanner(file);
System.out.println(input);
System.out.println(input.hasNext());
while(input.hasNextLine()) {
lines.add(input.nextLine());
}
} catch (Exception ex) {
System.out.println("Error: "+ ex);
}
calcluateCharCountAlpha();
}
Upvotes: 0
Reputation: 726629
This block has two paths through it:
try {
input = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.println("file not found" + file);
}
One is the path when there is no exception, and the other one is when there is a FileNotFoundException
.
The first path is fine: input
gets a valid Scanner
at the end of this path. The second path, however, leaves input
unchanged, i.e. set to null
.
To deal with this problem, remove try
/catch
, and let exception propagate out of the constructor. A very important rule of exception handling is that your code should not catch exceptions that it is not prepared to handle. In this case, your code cannot do anything about the missing file, because fileName
comes from the outside. Therefore, throwing the exception is the right thing to do:
FileExaminer(String filename) throws FileNotFoundException {
...
Scanner input = new Scanner(file);
...
}
Upvotes: 1
Reputation: 393851
If you get a FileNotFoundException
, you can't just catch it, print an error message, and proceed as if nothing happened. If that exception is thrown, it means your input
variable is not initialized, and you can't use it.
It would make more sense to put all the code that uses input
inside the try block:
try {
input = new Scanner(file);
System.out.println(input);
System.out.println(input.hasNext());
while(input.hasNextLine()) {
lines.add(input.nextLine());
}
} catch (FileNotFoundException e) {
System.out.println("file not found" + file);
}
Upvotes: 1