Here I am simply attempting to read lines from a saved file and then display them in a JTextArea.
NOTE: The JTextArea that I am attempting to display to is tested and working properly so the issue is not there.
try
{
File ScoreFile = new File("ScoreFile.FILE");
FileInputStream Read1 = new FileInputStream(ScoreFile);
InputStreamReader Read2 = new InputStreamReader(Read1);
BufferedReader ReadIt = new BufferedReader(Read2);
String score = ReadIt.readLine();
String score1 = ReadIt.readLine();
GraphicGameBoard.topScoreDisplay.setText(score + "\n");
GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");
ReadIt.close();
Read2.close();
Read1.close();
}
catch (Exception X) { System.out.print("Oops, Can't Load.");}
This will catch the exception every time. I have determined that if I remove the attempt to set the text in the topScoreDisplay, it will save the data from the file properly into the score and score1 variables without catching the exception.
I have tried many scenarios and they all fail for a different reason.
1: This fails because score and score1 have not been initialized outside of the try/catch but inside the try/catch the variables are successfully storing the data as the System.out.print shows. If I move the System.out.print outside of the try/catch then it will not print.
try
{
File ScoreFile = new File("ScoreFile.FILE");
FileInputStream Read1 = new FileInputStream(ScoreFile);
InputStreamReader Read2 = new InputStreamReader(Read1);
BufferedReader ReadIt = new BufferedReader(Read2);
String score = ReadIt.readLine();
String score1 = ReadIt.readLine();
System.out.print(score + "\n" + score1 + "\n");
ReadIt.close();
Read2.close();
Read1.close();
}
catch (Exception X) { System.out.print("Oops, Can't Load.");}
GraphicGameBoard.topScoreDisplay.setText(score + "\n");
GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");
2: If I initialize the variables before the try/catch then the System.out.print will work with the correct information either inside or outside the try/catch. If the .setText is inside, it will catch the exception. If it is outside it will cause a NPE.
String score;
String score1;
try
{
File ScoreFile = new File("ScoreFile.FILE");
FileInputStream Read1 = new FileInputStream(ScoreFile);
InputStreamReader Read2 = new InputStreamReader(Read1);
BufferedReader ReadIt = new BufferedReader(Read2);
score = ReadIt.readLine();
score1 = ReadIt.readLine();
System.out.print(score + "\n" + score1 + "\n");
ReadIt.close();
Read2.close();
Read1.close();
}
catch (Exception X) { System.out.print("Oops, Can't Load.");}
GraphicGameBoard.topScoreDisplay.setText(score + "\n");
GraphicGameBoard.topScoreDisplay.setText(score1 + "\n");
So, I can save the file data into the variables and I can get it to display with a System.out.print. But I cannot use the variables to .setText. What am I doing wrong?