Reputation: 61
Building a small game by myself as a little project, I'm not very good with error handling and suchlike, so I'm getting a java.io.FileNotFoundException
error and I'm not sure how to proceed.
This is my first post here so I apologize for being so vague, I'm guessing I need to throw the exception or catch it in some way?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class GameOver extends Actor {
public static Counter highScore;
public static int currentScore;
/**
* Act - do whatever the gameOver wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act() {
displayScore();
if(Greenfoot.isKeyDown("space")) {
Greenfoot.setWorld(new City());
}
}
//This is where I am getting the error
private void displayScore() {
highScore = new Counter ("HIGHSCORE: ");
getWorld().addObject(highScore, 700, 50); //Add width/height
highScore.setLocation(City.WIDTH/2, City.HEIGHT/4);
currentScore = (0+City.score);
int topScore;
//The error is coming from this block of code
BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"));
topScore = Integer.parseInt(saveFile.readLine());
saveFile.readLine();
saveFile.close();
if (topScore < currentScore) {
FileWriter writer = new FileWriter("topScore.txt");
writer.write(currentScore);
writer.close();
}
highScore.setValue(topScore);
}
}
Upvotes: 0
Views: 471
Reputation: 1168
An alternative, and more robust approach (which would still involve error handling), would be to create a File for the target filepath, which would allow you to validate the existence of the file, if it was actually a readable file, etc, et al.
This would allow you to check for the state of the file and throw Exceptions that you define that comfortably pinpoint specifically at which point the application is failing:
File file = new File(System.getProperty("user.dir")
+ "\\GameName\\Saves\\" + textsave.txt");
if(!f.exists()) {
f.getParentFile().mkdirs();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
And then manage your FileWriter
as you were previously, or go a step further and write code to deal with reading from/writing to a File:
// just as an example - not complete, runnable code
BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
FileWriter fw = new FileWriter(file, append); // append is a boolean flag to overwrite/not overwrite the entire content of the file
BufferedWriter bw = new BufferedWriter(fw);
bw.write(currentScore); // using your code
Hope this helps! Might be overkill, might not be :)
Upvotes: 0
Reputation: 1534
If you want to figure out why it cannot find the file, put this in your code:
System.out.println(new File("TextSave.txt").getAbsolutePath());
You will see where it's trying to open the file.
If you want to handle the error properly, wrap the IO operations in try/catch block:
try(BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"))) {
topScore = Integer.parseInt(saveFile.readLine());
saveFile.readLine();
} catch(IOException e) {
// log the error or show error message here
}
Upvotes: 0
Reputation: 166
Your question being "how to catch the exception", here's an answer :
//This is where I am getting the error
private void displayScore() {
highScore = new Counter ("HIGHSCORE: ");
getWorld().addObject(highScore, 700, 50); //Add width/height
highScore.setLocation(City.WIDTH/2, City.HEIGHT/4);
currentScore = (0+City.score);
int topScore;
try{
//The error is coming from this block of code
BufferedReader saveFile = new BufferedReader(new FileReader("TextSave.txt"));
topScore = Integer.parseInt(saveFile.readLine());
saveFile.readLine();
saveFile.close();
if (topScore < currentScore) {
FileWriter writer = new FileWriter("topScore.txt");
writer.write(currentScore);
writer.close();
}
highScore.setValue(topScore);
catch(FileNotFoundException e){
// Handle your exception here, like print a message 'the file XXX couldn't be read'
}
}
or you can just pass it to the calling function and handle it there :
private void displayScore() throws FileNotFoundException {
...
}
Upvotes: 1
Reputation: 65
To solve this issue I have 2 suggestions
Upvotes: 0