Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (553)
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  
  [libGDX] Error with Actor  (Read 837 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-07-06 15:12:44 »

Hey,

I'm trying to figure out how to use actors now and due to google not giving me any good tutorials except for this github code ( https://github.com/minimaldevelop/libgdx-scene2d-demo-game/blob/master/Libgdx-Scene2d-Game/src/com/minimaldevelop/libgdxgame/actors/FallingMan.java ) I've had to guess most of this.

The error I'm getting is...

1  
2  
3  
4  
Exception in thread "LWJGL Application" java.lang.NullPointerException
   at screens.Screen_Game2.render(Screen_Game2.java:83)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)

...which doesn't tell me anythingother than that there is something wrong somewhere in the code below.

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  
package actors;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.scenes.scene2d.Actor;

import core.Core_Resources;

public class Player extends Actor
{
   private Body actorBody;
   private int keyFrameTimer = 0;
   private int keyFrame = 0;
   private int direction = 0; // Down(0), Left(1), Right(2), Up(3), Down-Left(4), Down-Right(5), Up-Left(6), Up-Right(7)
 
   public Player()
   {
      setX(20);
      setY(20);
   }
   
   public void draw(SpriteBatch batch)
   {
      setPosition(actorBody.getPosition().x, actorBody.getPosition().y);
     
      keyFrameTimer++;
     
      if(keyFrameTimer >= 30)
      {
         batch.draw(Core_Resources.entities[0][direction][keyFrame], getX(), getY(), Core_Resources.entities[0][direction][keyFrame].getWidth(), Core_Resources.entities[0][direction][keyFrame].getHeight());
         keyFrameTimer = 0;
      }
   }
}


Can anyone see what I'm doing wrong in this?



Here is the main class in case that somehow has anything to do with it:
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  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
package screens;

import actors.Player;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class Screen_Game2 implements ApplicationListener
{
   private TiledMap map;
   private TiledMapRenderer renderer;
   private OrthographicCamera camera;
   private BitmapFont font;
   private SpriteBatch batch;
   private Player player;
   
   @Override
   public void create()
   {      
      float w = Gdx.graphics.getWidth();
      float h = Gdx.graphics.getHeight();

      camera = new OrthographicCamera();
      camera.setToOrtho(false, (w/h) * 10, 10);
     
      //Set the starting view location. (0,0) is in the bottom left corner.
     camera.position.x = 0;
      camera.position.y = 125;
     
      camera.update();

      font = new BitmapFont();
      batch = new SpriteBatch();

      map = new TmxMapLoader().load("assets/Maps/Map_01.tmx");
      renderer = new OrthogonalTiledMapRenderer(map, 1f / 32f);
   }

   @Override
   public void render()
   {
      //Sets the background color and then clears the screen.
     Gdx.graphics.getGL20().glClearColor(0f, 0f, 0f, 0f);
      Gdx.graphics.getGL20().glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
     
      //This will exit fullscreen mode.
     if(Gdx.input.isKeyPressed(Keys.ESCAPE))
      {
         System.exit(0);
      }
      //Movement Controls
     if(Gdx.input.isKeyPressed(Keys.LEFT) || Gdx.input.isKeyPressed(Keys.A))
      {
         camera.position.x -= 0.05;
      }
      if(Gdx.input.isKeyPressed(Keys.RIGHT) || Gdx.input.isKeyPressed(Keys.D))
      {
         camera.position.x += 0.05;
      }
      if(Gdx.input.isKeyPressed(Keys.UP) || Gdx.input.isKeyPressed(Keys.W))
      {
         camera.position.y += 0.05;
      }
      if(Gdx.input.isKeyPressed(Keys.DOWN) || Gdx.input.isKeyPressed(Keys.S))
      {
         camera.position.y -= 0.05;
      }

      camera.update();
      renderer.setView(camera);
      renderer.render();
      batch.begin();
      font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
      player.draw(batch);
      batch.end();
   }

   @Override
   public void dispose ()
   {
      map.dispose();
      font.dispose();
      batch.dispose();
   }

   @Override
   public void pause()
   {
      //Called just before dispose() on desktop.
  }

   @Override
   public void resize(int arg0, int arg1)
   {
      //Called once after the create() method and every time the game screen is resized.
  }

   @Override
   public void resume()
   {
      //Not used on desktop.
  }
}
Offline Phased
« Reply #1 - Posted 2013-07-06 15:19:05 »

You should look into understanding stack traces, this is a simple error
1  
2  
Exception in thread "LWJGL Application" java.lang.NullPointerException
   at screens.Screen_Game2.render(Screen_Game2.java:83)


we can see its a null pointer exception.
the next line says exactly where it is, inside Screen_Game2 inside the render method and the error is at line 83

we can tell one of the variables is null as if the crash was caused by something in the player class you would get this instead

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
//What thread the exception was located in, and what type of error
Exception in thread "LWJGL Application" java.lang.NullPointerException

//this line would say it is inside the render method IF IT WAS THE CAUSE
 at pathToPlayer.Player.render(Player.java:someLineNumberHere)

//player.render is called within this class at line 83
  at screens.Screen_Game2.render(Screen_Game2.java:83)

//more sub classes that it went through to get to the Screen_Game2 loop
  at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:204)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:112)



skipping to your problem:

line 83 is
1  
player.draw(batch);


either player or batch is not initialized, I can see the player is not initialized.

read up on stack traces so you can solve simple errors like this without assistance.

EDIT: more detail explanation
Offline Phased
« Reply #2 - Posted 2013-07-06 15:36:01 »

also, as a side note: correct me if wrong, I really have not used LibGDX, only played around with it for a hour or 2, So I'm not sure if the spritebatcher sets the font to be rendered after images or anything, but I am doubting that.

1  
2  
3  
4  
      batch.begin();
      font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
      player.draw(batch);
      batch.end();


you probably want the FPS drawn infront of the player instead of behind the player, so re order the render.

1  
2  
3  
4  
      batch.begin();
      player.draw(batch);
      font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
      batch.end();
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2013-07-07 02:49:13 »

Phased right. You never initialize the Player instance.

Also fix your naming convention.

Offline tyeeeee1
« Reply #4 - Posted 2013-07-07 14:57:36 »

Phased right. You never initialize the Player instance.

Also fix your naming convention.

Yeah.... I feel completely retarded for missing that. >.<

Huh? What's wrong with it?
Offline ReBirth
« Reply #5 - Posted 2013-07-08 02:01:09 »

Underscore and number. Google for Java camel convention.

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.

CopyableCougar4 (24 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (33 views)
2014-08-08 02:01:56

Norakomi (42 views)
2014-08-06 19:49:38
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!