Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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 external class error  (Read 2317 times)
0 Members and 1 Guest are viewing this topic.
Offline vastmind
« Posted 2017-02-25 17:36:19 »

Hi! I'm trying to create an external card class but I'm getting an error.
On my thoughts the problem is in myBatch, line 64

Main class
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  
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;



public class Main extends ApplicationAdapter {

   SpriteBatch myBatch;
   ShapeRenderer sr;
   int tileSize = 32;
   Card card1;
   Texture tex;




   @Override
   public void create () {


      myBatch = new SpriteBatch();
      sr = new ShapeRenderer();
      tex = new Texture(Gdx.files.internal("ball.png"));
      card1 = new Card(tex);



      }

   public void grid(){

      int height = Gdx.app.getGraphics().getHeight();
      int width = Gdx.app.getGraphics().getWidth();

      for (int i = 0; i <= width; i = i + tileSize) {

         sr.rectLine(i, height, i, 0,2, Color.BLACK, Color.BLACK);
      }

      for (int j = 0; j <= width; j = j + tileSize){

         sr.rectLine(width, j, 0, j,2, Color.BLACK, Color.BLACK);
      }

   }

   @Override
   public void render () {

      Gdx.gl.glClearColor(20/255f, 40/255f, 100/255f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);




      myBatch.begin();
      card1.draw(myBatch);
      sr.begin(ShapeRenderer.ShapeType.Filled);
      grid();
      sr.end();
      myBatch.end();


   }



   @Override
   public void dispose () {

      sr.dispose();

   }
}


Card class
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
package com.mygdx.game;


import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class Card extends Sprite {

    public Card(Texture myTexture) {

        //myTexture = new Texture(Gdx.files.internal("ball.png"));

    }
}


An error:

Exception in thread "LWJGL Application" java.lang.NullPointerException
   at com.badlogic.gdx.graphics.g2d.SpriteBatch.flush(SpriteBatch.java:962)
   at com.badlogic.gdx.graphics.g2d.SpriteBatch.end(SpriteBatch.java:183)
   at com.mygdx.game.Main.render(Main.java:68)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:225)
   at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)

Process finished with exit code 0
Offline CJC

Junior Devvie


Medals: 9
Exp: 7 years


Just press buttons until it works.


« Reply #1 - Posted 2017-02-25 18:44:29 »

The sprite's texture is never set. Let's break this apart line by line.

The error is thrown here:
1  
myBatch.end();


But like you, my suspicion is that it is really thrown because of this line:
1  
card1.draw(myBatch);


Which means that either card1, myBatch, or something used in the end/draw methods is null.

Well, we can see from your create method that card1 and myBatch are not null.
1  
2  
3  
4  
5  
6  
7  
@Override
public void create () {
     myBatch = new SpriteBatch();
     sr = new ShapeRenderer();
     tex = new Texture(Gdx.files.internal("ball.png"));
     card1 = new Card(tex);
}


My assumption (wtihout looking at the code) is that the end method (line 68) is trying to do all of the rendering at once, and so it's simply catching what was null from the draw method. Let's look at the draw method.

(Taken from the Sprite class)
1  
2  
3  
public void draw (Batch batch) {
     batch.draw(texture, getVertices(), 0, SPRITE_SIZE);
}


it is drawing the texture of the sprite. The constructor for Sprite takes in a texture as a parameter, however in your card class, while you do take in a texture parameter, you don't pass it to the Sprite.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
package com.mygdx.game;


import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class Card extends Sprite {

    public Card(Texture myTexture) {

        //myTexture = new Texture(Gdx.files.internal("ball.png"));

    }
}


The null pointer is being thrown because the texture is null. You take it, and literally do nothing with it. What you want to do, is to pass the texture you take in as a parameter in your Card class and use it by calling something like
1  
    super(myTexture);

If you're not familiar with what that does, I must urge you to pause your libGDX development and learn a bit more Java code before you go any further.

I would also ask, why exactly you are making a class that extends sprite, and then has nothing in it? It's basically just a typealias at this point (which Java doesn't technically have). Why not just have card1 be a type of sprite, and skip the unnecessary Card class (less files usually means less confusion, and always means less space taken up).
Offline vastmind
« Reply #2 - Posted 2017-02-25 19:47:27 »

Quote
I would also ask, why exactly you are making a class that extends sprite, and then has nothing in it? It's basically just a typealias at this point (which Java doesn't technically have). Why not just have card1 be a type of sprite, and skip the unnecessary Card class (less files usually means less confusion, and always means less space taken up).
I just wanted to create an external class just for practice. Thank you.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CJC

Junior Devvie


Medals: 9
Exp: 7 years


Just press buttons until it works.


« Reply #3 - Posted 2017-02-25 20:38:08 »

Fair enough. Did that solution work for you?
Offline vastmind
« Reply #4 - Posted 2017-02-26 03:44:38 »

Yes it did, it works without an error but draws a black texture.
I've found the reason: if I comment out the "grid()" method between sr.begin and sr.end, the texture shows up. But I have no idea how to fix it. May be it is because of frame buffer, opengl or something else.
I've solved it! Just used another myBatch.begin() and myBatch.end().
Offline CJC

Junior Devvie


Medals: 9
Exp: 7 years


Just press buttons until it works.


« Reply #5 - Posted 2017-02-26 13:58:50 »

Cool. Glad you were able to fix it!
Offline vastmind
« Reply #6 - Posted 2017-02-27 10:47:14 »

I've found another solution:

Just created another yet card class:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
package com.mygdx.game;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.Texture;


public class CardTwo {

    public void DrawCardTwo(SpriteBatch sprBatch, Texture myTexture){

        sprBatch.draw(myTexture, 300, 300);

    }

}

Main:
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  
package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;



public class Main extends ApplicationAdapter {

   SpriteBatch myBatch;
   ShapeRenderer sr;
   int tileSize = 32;
   Card card1;
   Texture tex, tex2;
   CardTwo card2;





   @Override
   public void create () {


      myBatch = new SpriteBatch();
      sr = new ShapeRenderer();
      tex = new Texture(Gdx.files.internal("ball.png"));
      tex2 = new Texture(Gdx.files.internal("badlogic.jpg"));
      card1 = new Card(tex);
      card2 = new CardTwo();



      }

   public void grid(){

      int height = Gdx.app.getGraphics().getHeight();
      int width = Gdx.app.getGraphics().getWidth();

      for (int i = 0; i <= width; i = i + tileSize) {

         sr.rectLine(i, height, i, 0,2, Color.BLACK, Color.BLACK);
      }

      for (int j = 0; j <= width; j = j + tileSize){

         sr.rectLine(width, j, 0, j,2, Color.BLACK, Color.BLACK);
      }

   }

   @Override
   public void render () {

      Gdx.gl.glClearColor(20/255f, 40/255f, 100/255f, 1);
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

      myBatch.begin();
      sr.begin(ShapeRenderer.ShapeType.Filled);
      grid();
      sr.end();
      myBatch.end();


      myBatch.begin();
      card1.draw(myBatch);
      card1.setPosition(100,100);
      myBatch.end();


      myBatch.begin();
      card2.DrawCardTwo(myBatch, tex2);
      myBatch.end();

   }



   @Override
   public void dispose () {

      sr.dispose();

   }
}

I'm excited). Thanks for help.
Offline dime26

JGO Ninja


Medals: 60
Projects: 7
Exp: 12 years


Should traffic wardens be armed?


« Reply #7 - Posted 2017-03-02 00:03:51 »

A little off topic but it would be better to keep an array of cards rather than creating a new variable for each new one.
Pages: [1]
  ignore  |  Print  
 
 

 
Ecumene (107 views)
2017-09-30 02:57:34

theagentd (134 views)
2017-09-26 18:23:31

cybrmynd (244 views)
2017-08-02 12:28:51

cybrmynd (236 views)
2017-08-02 12:19:43

cybrmynd (237 views)
2017-08-02 12:18:09

Sralse (251 views)
2017-07-25 17:13:48

Archive (863 views)
2017-04-27 17:45:51

buddyBro (1006 views)
2017-04-05 03:38:00

CopyableCougar4 (1564 views)
2017-03-24 15:39:42

theagentd (1372 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!