For reading lined from a file:
try{
BufferedReader loadPlayerPositions = new BufferedReader(new FileReader("/media/KEI-SAMA/CS11/JOKE CS11/KnowGoMilestone5/playerpositions.txt"));
String line = loadPlayerPositions.readLine();
while (<strong>line!=null</strong>) {
System.out.println("Line read: "+line);
<strong>line = loadPlayerPositions.readLine();</strong>
}
} catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
}
With that way you read each line.
Do some checking in case the file doesn't have all 4 lines that you want.
You get the exception at that line: info[0] = Players.nameOfPlayer1.toString(); probably becausePlayers.nameOfPlayer1 is null
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Put a e.printstacktrace at the catch block, post your new code with the errors that you get
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
I tried putting e.printStackTrace() but it didn't accept it saying that "void" cannot be used. When i checked, printStackTrace() was "void" after all. I tried using getMessage() and I got a message saying "Stream closed" when I tried running the program again.
catch (IOException e){
JOptionPane.showMessageDialog(null, "IOException: " +
e.getMessage());
}
catch (IOException e){
System.out.println(e.getMessage()); //returns String
JOptionPane.showMessageDialog(null, "IOException: " +
e.getMessage());
e.printStackTrace(); // is void
}
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
for (int l = 0; l < info.length; l++){
info[l] = loadPlayerPositions.readLine();
info[0] = Players.nameOfPlayer1.toString();
info[1] = Players.nameOfPlayer2.toString();
info[2] = Players.loadplayer1currentblock;
info[3] = Players.loadplayer2currentblock;
Players.loadCurrentPositions();
Players.currentPositions();
<strong>loadPlayerPositions.close();</strong>
}
You are inside the loop, you close the BufferedReader and when the loop runs again you try to read from aclosed BufferedReader. When you close the BufferedReader you cannot read from it again. Close it after you are done reading.
Also I believe that you should check if the line read:
info[l] = loadPlayerPositions.readLine();
is not null in case the file has less than 4 line
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448