I have a text file named leaderboard.txt, I load it up as a resource stream and read the contents to a string (it's a short text file) then I replace something in the string after that I want to create a new leaderboard.txt file in the same location or overwrite the previous leaderboard.txt text file.
Here is my code for overwriting the file, but it throws a java.lang.NullPointerException
package tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import screens.PlayMenu;
public class Leaderboard
{
private static BufferedReader leaderboardIn;
public static void checkLeaderBoard(int time, int currentLevel)
{
try
{
InputStream filePath = Leaderboard.class
.getResourceAsStream("/Other/Leaderboard.txt");
leaderboardIn = new BufferedReader(new InputStreamReader(filePath));
String input = "";
for (int level = 1; level < currentLevel; level++)
input = leaderboardIn.readLine() + '\n';
String line = leaderboardIn.readLine();
int leaderboardTime = Integer.parseInt(line.substring(0,
line.indexOf(' ')));
if (leaderboardTime > time)
{
line = time + " " + PlayMenu.playerName.toUpperCase() + '\n';
input += line;
while ((line = leaderboardIn.readLine()) != null)
input += line + '\n';
PrintWriter fileOut =
new PrintWriter(
new File(Leaderboard.class.getResource("/Trial and Error/resources/Other/Leaderboard.txt").getPath()));
fileOut.write(input);
fileOut.close();
}
filePath.close();
leaderboardIn.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Here's a picture of where my actual text file is:

Any kind of help would be appreciated.
Edit Here's the stack trace (which is also in the image)
Exception in thread "Thread-0" java.lang.NullPointerException
at tools.Leaderboard.checkLeaderBoard(Leaderboard.java:39)
at entities.Player.onExit(Player.java:91)
at entities.Player.update(Player.java:38)
at screens.GameState.update(GameState.java:27)
at main.Game.update(Game.java:62)
at main.Game.run(Game.java:111)
at java.lang.Thread.run(Unknown Source)
As I can see stack trace, which is pointing at
Line 39Please write following code and see their output.If last one returns
null, then resource with the name is not found.Additionally, Try following
instead of