Reputation: 117
try {
inFile = new Scanner(file);
}
catch (FileNotFoundException e) {
System.out.println("FileNotFoundException");
}
I have this code. However, after the try/catch statement I have the following:
while(inFile.hasNext()) {
}
The compiler is telling me that I have not initialized inFile. Do I need to put all of my code within the try/catch? What am I doing wrong?
Upvotes: 1
Views: 2011
Reputation: 3560
Initialize inFile:
Scanner inFile = null;
Edit:
As others have mentioned, you should be careful that you could potentially get a NullPointerException in your while loop. You should consider moving your while loop into the try block as well:
Scanner inFile = null;
...
try {
inFile = new Scanner(file);
while(inFile.hasNext()) {
}
}
catch (FileNotFoundException e) {
System.out.println("FileNotFoundException");
}
Upvotes: 2
Reputation: 55449
If you are getting a compiler error, you probably need to initialize inFile
to null.
Note that later in your code you shouldn't assume that inFile is not null, you should always check it:
e.g.
if (inFile != null) {
while (inFile.hasNext()) {
...
}
}
Upvotes: 2
Reputation: 49087
No your code is fine, however local variables must be intialized so you have to set Scanner inFile = null;
And it is correct what you already have done, because if you move the local variable inside the try/catch statement you will not have access to it because of the scope.
Scanner inFile = null;
try {
inFile = new Scanner(file);
//more code
} catch (Exception e) {
//exception code
}
while (inFile.nextLine()) {
//loop code
}
If you had an instance variable it would have been automatically set to null, but in this case you have a local variable and then objects need to be initialized before you use it.
Upvotes: 0
Reputation: 822
Yes, as others have said, initialize inFile to null
However, you will also need to check that inFile actually points a valid file and is not NULL when you get to your loop e.g.
while(inFile!=null && inFile.hasNext()) {
}
Otherwise, you perhaps want to place the whole try-catch in a different loop to make the user select another file? Or just exit the program if the file is invalid? The missing element to the question is how you wish to handle invalid files. Does the program exit or re-prompt the user?
Upvotes: -1
Reputation: 2795
Yes, you do. If an exception is raised the runtime will print "FileNotFoundException" and will keep running, although inFile will not have been initialized.
You should make the program return when stumbling upon this exception, or else do your operations on infile
only when you are sure that it has been initialized correctly.
Upvotes: 1
Reputation: 340763
The compiler is complaining because if new Scanner()
throws FileNotFoundException
, inFile
won't be initialized (BTW very unfortunate variable name). Your loop should be inside try
block, which will also increase readability.
try {
Scanner inFile = new Scanner(file);
while(inFile.hasNext()) {
//...
}
inFile.close();
}
catch (FileNotFoundException e) {
System.out.println("FileNotFoundException");
}
Upvotes: 2
Reputation: 10151
The reason for the warning is that you should set the Scanner to null initially. However you should also move the while loop inside the try block, because if the exception is thrown you don't want that while loop to execute (because inFile will be null).
Upvotes: 0
Reputation: 4593
The try block is not in the same scope as your while loop. Put the while loop inside the try block.
Upvotes: 0