Mestru
Mestru

Reputation: 427

Android build crashes when looping through an 2D array - libGDX

I will quickly get to the point.
I'm working on my little game, using libgdx library and I have following problem.
Everytime I'm trying to execute this loop:

for (int row = 0; row < currentMapHeightTiles; row++) {
        for (int col = 0; col < 9; col++) {
            if (currentMap[row][col] == 4 && (currentMapHeight - position.y) >  row * 40 && (currentMapHeight - position.y) < (row+1) * 40
                    && position.x > col * 40 && position.x < (col+1) * 40) {
                Gdx.app.log("Wall Collision", "found");
                collides = true;
            } else if (currentMap[row][col] == 3 && (currentMapHeight - position.y) >  row * 40 && (currentMapHeight - position.y) < (row+1) * 40
                    && position.x > col * 40 && position.x < (col+1) * 40) {
                Gdx.app.log("Gate Collision", "found");
                collides = true;
            }
        }
    }

my android build crashes. It works like charm on desktop version, even compiles on android, but as soon as I'm executing it, my phone just says that "My app stopped working".

Simplified version of it doesn't work as well:

for (int row = 0; row < currentMapHeightTiles; row++) {
        for (int col = 0; col < 9; col++) {
            if (currentMap[row][col] == 4) {

            } else if (currentMap[row][col] == 3) {

            }
        }
    }

and this one doesn't do anything, it just iterates through an array.

2D Array is created with this method:

public int[][] getMap() throws Exception {

    String line;

    Scanner scanner = new Scanner(new File("data/level_one.txt"));

        for (int row = 0; row < mapH; row++) {
            line = scanner.nextLine();
            String[] rowA = line.split(" ");
            for (int col = 0; col < 9; col++) {
                levelMap[row][col] = Integer.parseInt(rowA[col]);
            }
        }
    return levelMap;
}

Logs:

    06-03 15:34:42.195    1293-1293/com.mestru.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:42.265    1293-1293/com.mestru.game.android D/dalvikvm﹕ Added shared lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:42.265    1293-1293/com.mestru.game.android D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8, skipping init
    06-03 15:34:42.425    1293-1293/com.mestru.game.android D/﹕ HostConnection::get() New Host Connection established 0xb8466920, tid 1293
    06-03 15:34:42.965    1293-1293/com.mestru.game.android I/AndroidInput﹕ sensor listener setup
    06-03 15:34:43.215    1293-1293/com.mestru.game.android W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    06-03 15:34:43.235    1293-1293/com.mestru.game.android D/OpenGLRenderer﹕ Enabling debug mode 0
    06-03 15:34:43.385    1293-1306/com.mestru.game.android D/﹕ HostConnection::get() New Host Connection established 0xb85aba30, tid 1306
    06-03 15:34:43.425    1293-1306/com.mestru.game.android W/GL2JNIView﹕ creating OpenGL ES 2.0 context
    06-03 15:34:43.545    1293-1306/com.mestru.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:43.545    1293-1306/com.mestru.game.android D/dalvikvm﹕ Shared lib '/data/app-lib/com.mestru.game.android-1/libgdx.so' already loaded in same CL 0xb1db73c8
    06-03 15:34:43.555    1293-1306/com.mestru.game.android I/GL2﹕ all initialized 2
    06-03 15:34:43.565    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL renderer: Android Emulator OpenGL ES Translator (AMD Radeon R9 200 Series)
    06-03 15:34:43.565    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL vendor: Google (ATI Technologies Inc.)
    06-03 15:34:43.575    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL version: OpenGL ES 2.0 (4.4.13283 Compatibility Profile Context 14.501.1003.0)
    06-03 15:34:43.625    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL extensions: GL_EXT_debug_marker GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint GL_OES_texture_float GL_OES_texture_float_linear GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_packed_depth_stencil GL_OES_vertex_half_float GL_OES_texture_npot GL_OES_rgb8_rgba8
    06-03 15:34:43.625    1293-1306/com.mestru.game.android E/EGL_emulation﹕ [getAttribValue] Bad attribute idx
    06-03 15:34:43.625    1293-1306/com.mestru.game.android E/EGL_emulation﹕ tid 1306: eglGetConfigAttrib(605): error 0x3004 (EGL_BAD_ATTRIBUTE)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android E/EGL_emulation﹕ [getAttribValue] Bad attribute idx
    06-03 15:34:43.635    1293-1306/com.mestru.game.android E/EGL_emulation﹕ tid 1306: eglGetConfigAttrib(605): error 0x3004 (EGL_BAD_ATTRIBUTE)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ framebuffer: (5, 6, 5, 0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ depthbuffer: (16)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ stencilbuffer: (0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ samples: (0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ coverage sampling: (false)
    06-03 15:34:43.675    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed meshes/app: { }
    06-03 15:34:43.675    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed textures/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed cubemap/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed shaders/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed buffers/app: { }
    06-03 15:34:43.695    1293-1306/com.mestru.game.android I/MathsGame﹕ created
    06-03 15:34:44.185    1293-1306/com.mestru.game.android W/System.err﹕ java.io.FileNotFoundException: /data/level_one.txt: open failed: ENOENT (No such file or directory)
    06-03 15:34:44.185    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
    06-03 15:34:44.285    1293-1306/com.mestru.game.android W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
    06-03 15:34:44.285    1293-1306/com.mestru.game.android W/System.err﹕ at java.util.Scanner.<init>(Scanner.java:158)
    06-03 15:34:44.305    1293-1306/com.mestru.game.android W/System.err﹕ at java.util.Scanner.<init>(Scanner.java:138)
    06-03 15:34:44.305    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.screens.levels.Level_One.getMap(Level_One.java:24)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.gameWorld.GameWorld.initLevels(GameWorld.java:45)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.gameWorld.GameWorld.<init>(GameWorld.java:35)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.screens.GameScreen.<init>(GameScreen.java:19)
    06-03 15:34:44.425    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.game.MathsGame.create(MathsGame.java:14)
    06-03 15:34:44.425    1293-1306/com.mestru.game.android W/System.err﹕ at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1512)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.Posix.open(Native Method)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ ... 11 more
    06-03 15:34:44.885    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 232K, 9% free 3153K/3452K, paused 50ms, total 53ms
    06-03 15:34:46.105    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 393K, 13% free 3253K/3716K, paused 41ms, total 42ms
    06-03 15:34:46.525    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 67K, 11% free 3402K/3796K, paused 40ms, total 40ms
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ show called
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ resize called
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ resize called
    06-03 15:34:54.915    1293-1306/com.mestru.game.android W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1ad9ba8)
    06-03 15:34:54.925    1293-1306/com.mestru.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 93
        Process: com.mestru.game.android, PID: 1293
        java.lang.NullPointerException
            at com.mestru.objects.Player.checkCollision(Player.java:143)
            at com.mestru.objects.Player.update(Player.java:56)
            at com.mestru.gameWorld.GameWorld.update_one(GameWorld.java:83)
            at com.mestru.gameWorld.GameWorld.update(GameWorld.java:71)
            at com.mestru.screens.GameScreen.render(GameScreen.java:32)
            at com.badlogic.gdx.Game.render(Game.java:46)
            at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:422)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
    06-03 15:34:58.145    1293-1306/com.mestru.game.android I/Process﹕ Sending signal. PID: 1293 SIG: 9

So yeah, conclusing - everytime I go through this array, my android build crashes. When I'm trying to use try/catch, it just doesn't check collision at all. Would use any help <3

Edit. Still can't get it to work :<

Edit2. Luckily with help I managed to make it work. I've replaced

 Scanner scanner = new Scanner(new File("data/level_one.txt"));

with

FileHandle file = Gdx.files.internal("data/level_one.txt");

Scanner scanner = new Scanner(file.readString());

and now it works like charm :)

Upvotes: 0

Views: 257

Answers (1)

Angel Angel
Angel Angel

Reputation: 21728

In this line of your code i thinks is one problem->

public int[][] getMap() throws Exception {

    String line;

    Scanner scanner = new Scanner(new File("data/level_one.txt"));

        for (int row = 0; row < mapH; row++) {
            line = scanner.nextLine();
            String[] rowA = line.split(" ");
            for (int col = 0; col < 9; col++) {
                levelMap[row][col] = Integer.parseInt(rowA[col]);
            }
        }
    return levelMap;
}

.

Scanner scanner = new Scanner(new File("data/level_one.txt"));

example:

Scanner scanner = new Scanner(Gdx.files.internal("data/level_one.txt").file());

read this and implement in your code:

https://github.com/libgdx/libgdx/wiki/File-handling#reading-from-a-file

https://github.com/libgdx/libgdx/wiki/File-handling#obtaining-filehandles

maybe this also is affecting in your code, I hope it will help, if not so notify me and delete the response

Upvotes: 1

Related Questions