简体   繁体   中英

LibGDX problem with loading Text File into Scanner

JAVA PROGRAM

I recently started using LibGDX to make a simple clone of "Pacman". However I am facing a issue with loading a text file into Scanner. Everything is okay, when I am running my Game in NetBeans. But the issue comes when I try to run it as a .jar file.

I've tried using BufferedReader, FileReader but none of them solved the issue...

File subor = Gdx.files.internal("HraciaPlocha/gamearea.txt").file();

Scanner sc = new Scanner(subor);

this.width = sc.nextInt() + 1; // Custom width
this.height = sc.nextInt() + 1; //Custom height

sc.nextLine();

    this.arena = new Block[width][height];

    for (int rows = 0; rows < this.arena.length - 1; rows++) {

        for (int columns = 0; columns < this.arena[rows].length - 1; columns++) {
            switch (sc.nextInt()) {
                case 1:
                    this.arena[rows][columns] = new Bariera(lavaXSur + 32 * columns, hornaYSur + 32 * rows);
                    break;
                case 2:
                    this.arena[rows][columns] = new KlasickaBodka(lavaXSur + 32 * columns, hornaYSur + 32 * riadky);
                    this.pridajBodku();
                    break;
                case 3:
                    this.arena[rows]columns] = new Zmurzuvac(lavaXSur + 32 * columns, hornaYSur + 32 * rows);
                    break;
                default:
                    this.arena[rows][columns] = null;
                    break;
            }

        }

    }

    sc.close();

    this.spustena = true;
}

I expect game to run when running from a .jar file but it gives me this Error:

mßj 09, 2019 3:31:43 PM com.rufo123.pacman.Hra create
SEVERE: null
java.io.FileNotFoundException: HraciaPlocha\gamearea.txt (System cannot find specified path)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.util.Scanner.<init>(Unknown Source)
        at com.rufo123.pacman.GameArea.nacitajArenu(GameArea.java:54)
        at com.rufo123.pacman.Hra.create(Hra.java:59)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplic
ation.java:149)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplicati
on.java:126)

Exception in thread "LWJGL Application" java.lang.NullPointerException
        at com.rufo123.pacman.GameArea.renderArena(GameArea.java:93)
        at com.rufo123.pacman.Hra.render(Hra.java:97)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplic
ation.java:225)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplicati
on.java:126)

I would be very glad if you help me solve this problem.

PS. If anything is unclear, just Ask about it. :)

I have tried the same as you and become the same error.

I'm not sure why the error appears, but I solved the problem like this:

FileHandle f = Gdx.files.internal("test/test.txt");

Scanner scanner = new Scanner(f.read());

while(scanner.hasNext()){
    System.out.println(scanner.nextLine());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM