Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
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 Entity error  (Read 629 times)
0 Members and 1 Guest are viewing this topic.
Offline ArthurSpark

Senior Newbie


Exp: 4-6 months


Jumping Cow Can Make Big Mess


« Posted 2014-04-07 18:04:13 »

Hello!

I made player class.Player class controls player and sets to render the player.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public class Player extends ObjectClass{
    Main1 game;
    Texture brick;
   public void player() {
//Set Texture of player
   brick = new Texture(Gdx.files.internal("glass.png"));
   }
   public void render(SpriteBatch batch) {
      //start drawing brick
      batch.begin();
      batch.draw(brick,0,0);
      batch.end();
   }  
}

Then i set PlayState class to render that entity
1  
2  
3  
4  
public void draw() {
      //draw player
      player.render(batch);
   }

And then it all goes to main class where all of stuff renders Cheesy
1  
2  
3  
4  
5  
6  
7  
8  
public void render() {
            Gdx.gl.glClearColor(1, 1, 1, 1);
            Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
            camera.update();
            batch.setProjectionMatrix(camera.combined);
            gsm.update(Gdx.graphics.getDeltaTime()); // update
            gsm.draw(); // Draw all things in PlayState
   }



But when i want run the game, i just see NullPointerException and error Player.render(Player.java:30).That is that line
1  
batch.draw(brick,0,0);

I really dont know what is problem.I had tried to fix it but really that error stays.
How can i fix that error?
Can someone help me Smiley

Team Spark || Coding Since 2012
Online matheus23

JGO Kernel


Medals: 113
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2014-04-07 18:07:05 »

you never Initialized the "batch" instance. You have to call
batch = new SpriteBatch(...)
somewhere in your initialization code. Smiley

See my:
    My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
Offline ArthurSpark

Senior Newbie


Exp: 4-6 months


Jumping Cow Can Make Big Mess


« Reply #2 - Posted 2014-04-08 15:10:22 »

Nop didn't work. It just shows NullPoinertException

Team Spark || Coding Since 2012
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2014-04-08 17:08:43 »

show us the create method of your libgdx game class

Offline ArthurSpark

Senior Newbie


Exp: 4-6 months


Jumping Cow Can Make Big Mess


« Reply #4 - Posted 2014-04-08 17:21:07 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
public class Main {

   public static void main(String[] args) {
      LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
      cfg.title = "Block";
      cfg.width = 700;
      cfg.height = 600;

      new LwjglApplication(new Main1(), cfg);
   }
}

Ok there is da code

Team Spark || Coding Since 2012
Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 139
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2014-04-08 17:24:03 »

Uh, that's the desktop launcher. Try the create() method of the class that extends Game or implements ApplicationListener.
Offline ArthurSpark

Senior Newbie


Exp: 4-6 months


Jumping Cow Can Make Big Mess


« Reply #6 - Posted 2014-04-08 17:27:28 »

Ups soory this is it

1  
2  
3  
4  
5  
6  
7  
8  
public void create() {      
   
    WIDTH = Gdx.graphics.getWidth(); // WIDTH
    HEIGHT = Gdx.graphics.getHeight(); // HEIGHT
      batch = new SpriteBatch();
     
      setScreen(new PlayState(this));
   }

Team Spark || Coding Since 2012
Online trollwarrior1
« Reply #7 - Posted 2014-04-08 17:53:18 »

Null pointer exception means that the object you're trying to access is null.

In other words, there is nothing there, therefore you're getting the exception..

I would suggest you to look at exception the other way around. They are not here to be annoying. They are here to be helpful.

Imagine that you didn't get the error just now, and instead nothing happened. I turned off exceptions in Eclipse at some time and I was so frustrated looking at code and why it didn't work for hours.. Without exceptions you would die.. Or stop programming all together..
Offline Sabomoth

Junior Devvie


Medals: 2
Exp: 2 years



« Reply #8 - Posted 2014-04-08 20:12:28 »

Make sure the texture is actually there?
Offline Gibbo3771
« Reply #9 - Posted 2014-04-08 20:17:13 »

Make sure the texture is actually there?

It would throw a file not found exception.

@OP See when it gives you the error, copy and paste that entire thing here.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online Endos
« Reply #10 - Posted 2014-04-08 22:27:01 »

The problem is that you have not initialized brick and is null.

Change
1  
2  
3  
4  
public void player() {
//Set Texture of player
   brick = new Texture(Gdx.files.internal("glass.png"));
   }


with
1  
2  
3  
4  
public Player() {
//Set Texture of player
   brick = new Texture(Gdx.files.internal("glass.png"));
   }


Note that public void player() is not a constructor.

Bored Birds - End with your boredom for Android
Retroships - Space Shooter for Android
Ultimate - 2D Side-Scrolling Platformer project
Offline seismic

Senior Devvie


Medals: 4
Projects: 1



« Reply #11 - Posted 2014-04-09 05:28:12 »

What Endos said.

Or you could call
1  
2  
Player player = new Player();
player.player();


somewhere in your other class, which looks funny Cheesy
Offline ArthurSpark

Senior Newbie


Exp: 4-6 months


Jumping Cow Can Make Big Mess


« Reply #12 - Posted 2014-04-09 05:59:18 »

thanks for help now it works Smiley

Team Spark || Coding Since 2012
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.

PocketCrafter7 (12 views)
2014-11-28 16:25:35

PocketCrafter7 (8 views)
2014-11-28 16:25:09

PocketCrafter7 (8 views)
2014-11-28 16:24:29

toopeicgaming1999 (75 views)
2014-11-26 15:22:04

toopeicgaming1999 (65 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
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!