0

I have a problem with my Java Game. When i want to export it, it wont load the World. I'll get an "Thread - 0" Exception. This is my function:

    private void loadWorld(String path){
    String file = World.class.getResourceAsStream(path).toString();
    file.toString();
    String[] tokens = file.split("\\s+");
    width = Utils.parseInt(tokens[0]);
    height = Utils.parseInt(tokens[1]);
    spawnX = Utils.parseInt(tokens[2]);
    spawnY = Utils.parseInt(tokens[3]);

    tiles = new int[width][height];
    for(int y = 0;y < height;y++){
        for(int x = 0;x < width;x++){
            tiles[x][y] = Utils.parseInt(tokens[(x + y * width) + 4]);
        }
    }
}

It needs to be loaded as a String since i need to split every String from it into an Array. What is the proper way to manage this?

EDIT: This is the Stack Trace:

C:\Users\User>java -jar C:\Users\User\Desktop\Game.jar
Exception in thread "Thread-0" java.lang.NullPointerException
    at dev.codenmore.tilegame.worlds.World.loadWorld(World.java:76)
    at dev.codenmore.tilegame.worlds.World.<init>(World.java:36)
    at dev.codenmore.tilegame.states.GameState.<init>(GameState.java:14)
    at dev.codenmore.tilegame.Game.init(Game.java:61)
    at dev.codenmore.tilegame.Game.run(Game.java:94)
    at java.lang.Thread.run(Unknown Source)
Gandalf1783
  • 11
  • 1
  • 8
  • All exceptions come with a type, a message and a stack trace allowing to know why and where they have been thrown. If you don't read it, and don't let us read it, you're shooting yourself in the foot. – JB Nizet Apr 18 '19 at 16:32
  • Hint: here's the javadoc of InputStream.toString(): https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString(). Here's the class allowing to read characters from a stream: https://docs.oracle.com/javase/8/docs/api/java/io/InputStreamReader.html – JB Nizet Apr 18 '19 at 16:36
  • And here's the explanation on what is a NullPointerException: https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – JB Nizet Apr 18 '19 at 16:37
  • I added the Stack Trace. The Error is on line getResourceAsStream(path) Do you know what to do? Nothing should be Null.... – Gandalf1783 Apr 18 '19 at 16:40
  • Most obviously the path is not valid. Try to to sout it and be sure to check the path if it is existing ... – Melvin Apr 18 '19 at 16:41
  • The javadoc of getResourceAsStream() explains what it returns, what its path argument should be, and when it returns null. You need to read that documentation, carefully. – JB Nizet Apr 18 '19 at 16:42
  • @Melvin17 what would the path be like? I used "worlds/world1.txt". When you open the jar file with 7zip, the worlds folder is on the root of the jar, – Gandalf1783 Apr 18 '19 at 16:42
  • Actually i added some Debug-Output and it says that the stream is equal to null. Now my question is still why and how to fix this problem. That would be great :D – Gandalf1783 Apr 18 '19 at 16:53
  • The path is not correct and, even if it was, you're not actually reading anything from the file. Calling toString() on a File path will only return the same string. –  Apr 18 '19 at 17:04
  • @Gandalf1783 It's a difference to load "worlds/world1.txt" or "/worlds/world1.txt". The first starts in the local directory and searches there a directory called worlds with a file world1.txt in in (relative path). The second searches from the root-directory for the directory worlds (absolute path). Please check this. – Melvin Apr 18 '19 at 17:06

2 Answers2

0

I did it! I used the following code to read the file: InputStream stream = World.class.getResourceAsStream("/worlds/world1.txt"); Maybe it was Buggy but at least it works!

Gandalf1783
  • 11
  • 1
  • 8
  • Great. Now you need to read from the Stream and get the text from the file. –  Apr 18 '19 at 17:07
  • @Benson99 the Stream worked all the time but it didnt found the resource. Thats because it didnt worked. I only needed to get the right way to load it. But thanks anyways for your help :D I will tick my answer as Solution tomorrow ;) – Gandalf1783 Apr 19 '19 at 12:21
0

always start with the debugger my friend. i would pop up the debugger to see what path is being placed here World.class.getResourceAsStream(path)

a null pointer exception will tell you that youre passing into your function a null value which it cannot handle. further down the call stack, you have

at dev.codenmore.tilegame.worlds.World.loadWorld(World.java:76)

which basically tells you where your nullpointerexception has occured. your error stack trace was a dead giveaway to what the problem was here.

Good luck in your project

Max Alexander Hanna
  • 3,388
  • 1
  • 25
  • 35