Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
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 533 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 20: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
Offline matheus23

JGO Kernel


Medals: 106
Projects: 3


You think about my Avatar right now!


« Reply #1 - Posted 2014-04-07 20: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 17: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 19: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 19: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: 128
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2014-04-08 19: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 19: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 19: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 Member


Medals: 1
Exp: 2 years



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

Make sure the texture is actually there?
Offline Gibbo3771
« Reply #9 - Posted 2014-04-08 22: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
Offline Endos
« Reply #10 - Posted 2014-04-09 00: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
Online seismic

Senior Member


Medals: 4
Projects: 1



« Reply #11 - Posted 2014-04-09 07: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 07:59:18 »

thanks for help now it works Smiley

Team Spark || Coding Since 2012
Pages: [1]
  ignore  |  Print  
 
 

 

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

The first screenshot will be displayed as a thumbnail.

pw (13 views)
2014-07-24 01:59:36

Riven (11 views)
2014-07-23 21:16:32

Riven (12 views)
2014-07-23 21:07:15

Riven (13 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (38 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (34 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!