Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Nullpointer exception at render  (Read 1456 times)
0 Members and 1 Guest are viewing this topic.
Offline Nanoc

Junior Newbie





« Posted 2012-12-17 22:17:26 »

Hi, im starting with libgdx on android and im trying to do some practices, im getting this error

E/AndroidRuntime( 1592): FATAL EXCEPTION: GLThread
E/AndroidRuntime( 1592): java.lang.NullPointerException
E/AndroidRuntime( 1592):        at gdx.game.Game.render(Game.java:81)
E/AndroidRuntime( 1592):        at com.badlogic.gdx.backends.android.AndroidGrap
hics.onDrawFrame(AndroidGraphics.java:449)
E/AndroidRuntime( 1592):        at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
E/AndroidRuntime( 1592):        at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)

Here is my code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
public class Game implements ApplicationListener {

    Stage stage;
    SpriteBatch spriteBatch;
    muñeco pnj;
    Image image;
    Touchpad touchpad;
    TextureRegion[] walkFrames;
    float stateTime;
    OrthographicCamera camera;
    TextureRegion currentFrame;
    Label lbl;
    JoystickScreen js;

    public void create() {
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
        pnj = new muñeco(getSprites("data/sprite.png", 3, 4));
        pnj.setAnims();
        touchpad = new Touchpad(20, skin);
        touchpad.setBounds(15, 15, 100, 100);

        stage.addActor(touchpad);

    }

    public void render() {

        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stateTime += Gdx.graphics.getDeltaTime();                       // #15
        if (touchpad.getKnobPercentX() > 50) {
            currentFrame = pnj.walkr.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentX() < -50) {
            currentFrame = pnj.walkl.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentY() > 50) {
            currentFrame = pnj.walku.getKeyFrame(stateTime, true);      // #16
        }
        if (touchpad.getKnobPercentY() < -50) {
            currentFrame = pnj.walkd.getKeyFrame(stateTime, true);      // #16
        }
        spriteBatch.begin();
        spriteBatch.draw(currentFrame, 100, 100);                         // #17
        spriteBatch.end();
        stage.draw();
    }

    public void resize(int width, int height) {
        stage.setViewport(width, height, true);
    }

    public void pause() {
    }

    public void resume() {
    }

    public void dispose() {
        stage.dispose();
    }

    public void escribir(String frase) {
        lbl.setText(frase);
    }

    public static TextureRegion[] getSprites(String filepath, int col, int fil) {
        Texture walkSheet = new Texture(Gdx.files.internal(filepath));
        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / col, walkSheet.getHeight() / fil);
        TextureRegion[] walkFrames = new TextureRegion[col * fil];
        int index = 0;
        for (int i = 0; i < col-1; i++) {
            for (int j = 0; j < fil-1; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        return walkFrames;
    }
}


The line that gives the error is:

spriteBatch.begin();

Sorry for that dirty code, i have no idea of what im doing wrong but im sure its a really noob mistake.

Hope some help and sorry for my bad english
Online Magn919
« Reply #1 - Posted 2012-12-17 22:30:43 »

It doesn't look like you are ever creating that SpriteBatch object, so the variable just hold a null value.

For every new problem, a new source of solutions has come to exist.
Offline Nanoc

Junior Newbie





« Reply #2 - Posted 2012-12-17 23:14:47 »

thanks that was a really stupid fail, but im still getting errors

E/AndroidRuntime( 1409): FATAL EXCEPTION: GLThread
E/AndroidRuntime( 1409): java.lang.NullPointerException
E/AndroidRuntime( 1409):        at com.badlogic.gdx.graphics.g2d.SpriteBatch.dra
w(SpriteBatch.java:730)
E/AndroidRuntime( 1409):        at gdx.game.Game.render(Game.java:67)
E/AndroidRuntime( 1409):        at com.badlogic.gdx.backends.android.AndroidGrap
hics.onDrawFrame(AndroidGraphics.java:449)
E/AndroidRuntime( 1409):        at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
E/AndroidRuntime( 1409):        at com.badlogic.gdx.backends.android.surfaceview
.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)

on the next line
spriteBatch.draw(currentFrame, 100, 100);

that textureregion should be ok, the getSprites() function its almost copy paste from here: http://code.google.com/p/libgdx/wiki/SpriteAnimation

Really thanks for the help and sorry for my bad english
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Magn919
« Reply #3 - Posted 2012-12-17 23:52:31 »

getSprites()?
In the code you posted before currentFrame isn't being instantiated either.

Edit: by the way, you really should check your code an extra time before posting more of these NullPointerException errors, because they are often easy enough to spot,
make sure you have a clear idea where your variables is instantiated.

For every new problem, a new source of solutions has come to exist.
Offline ReBirth
« Reply #4 - Posted 2012-12-18 02:44:07 »

I dont know what happened when you give null TextureRegion for SpriteBatch to draw but since exception raised from inside, your currentFrame maybe the ebil.

NB: if you want quicker method to make this work, use that basic Game class instead ApplicationListener.

Offline ReBirth
« Reply #5 - Posted 2012-12-18 14:17:50 »

First, are you sure it compiled? muneco's Animation fields aren't public.

EDIT: You delete the post after I reply? :O

Offline Nanoc

Junior Newbie





« Reply #6 - Posted 2012-12-18 20:11:18 »

I solved it myself and ofc it compiles with that code the currentframe only initializes when i drag the touchpad, i added a default frame and it works.

Thank u very much for the help i will be in this great forum, and again sorry for my poor english i try my best Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

theagentd (13 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!