Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (489)
Games in Android Showcase (112)
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  
  [SOLVED][libGDX] Dedicating a class to fill an array, while implementing screens  (Read 851 times)
0 Members and 1 Guest are viewing this topic.
Offline sipty

Junior Newbie





« Posted 2013-09-15 20:10:19 »

Hello everyone!

My situation is the following - I have a few screens and in one of them - GameScreen, I want to output data from a string array. The array is pretty big - well over 200 entries.

The problem is, when I try to fill the array in another class - TextFill, and then use it in GameScreen, java says it's of size 0.

If anyone could give me any insight on the matter, I would be grateful!
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 128
Projects: 4
Exp: 3 years



« Reply #1 - Posted 2013-09-16 01:11:43 »

Very very vague question, need specific code.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #2 - Posted 2013-09-16 07:32:04 »

Have you made getters/setters or are you accessing the (assuming arraylist because you said size) accessing the arraylist publicly? I'd recommend passing it in as a parameter to your outside classes, but some code would be nice to see.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SHC
« Reply #3 - Posted 2013-09-16 07:38:19 »

Are you saying that the size is zero after filling the arrays? Maybe this can happen if you have two threads, writing on one thread and checking on another. If this is not the case, you need to be more clear in your post.

Offline sipty

Junior Newbie





« Reply #4 - Posted 2013-09-16 08:19:13 »

Ok, let me try to shed some more light on the problem itself and explain to you my deduction process.

Here is a list of pretty much everything I did:

Originally I had the array in TextFill, filled it in TextFill and accessed it through a getter and also directly - tf.text.get(0); in GameScreen. That didn't work out.

I then created the array in GameScreen and tried filling it through setters/public access/getter to no avail.
 
Lastly, I did all of this, but the main class was in the role of the gamescreen, with no success.

Oh, and let's not forget I called/filled the array in TextFill's constructor/separate method/ both and it still did not work.

I know that the size of the array is 0, as I got that error message in the console - array size 0, alongside a null error message several times, but it didn't seem connected, which led me to believe it's not being called at all, thus the reason for the, as I like to think of it - straight to the point question.
 
This is the TextFill class in one of it's incarnations:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class TextFill {
   
   // declarations
  public ArrayList<String> text = new ArrayList<String>();
   private Dealer game;
   
   // constructor
  public void fill() {
     
      // fill the array
     game.text.add("Michael?");
      game.text.add("Michael?!");
      game.text.add("Michael...?");
     
      //TODO: add the rest of the text
  }
}


This is the GameScreen class with heavily omitted relevant 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  
public class GameScreen implements Screen {
   
   private TextFill tf;
   
   // constructor
  public GameScreen(final Dealer gam) {
      this.game = gam;
         
      // text
     tf = new TextFill();

       // Lot's of working declarations omitted from code.
  }

   // render
  @Override
   public void render(float delta) {
                // screen clear, camera update, batch preparation, camera adjustment
     
      // batch
     game.batch.begin();
         // text
        game.font.draw(game.batch, game.text.get(0), 350, 100);
                // picture/text drawing
     
      // end batch
     game.batch.end();
     
      // a ton of @Override methods
}


// And finally the main class, in which I tried the same things I did to GameScreen. This is just one of it's forms.
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  
public class D extends Game {
   
   public ArrayList<String> text = new ArrayList<String>();
   public TextFill tf;
   
   public void create() {
     
      tf = new TextFill();
      tf.fill();
     
      Texture.setEnforcePotImages(false);
      batch = new SpriteBatch();
      // using the default font
     font = new BitmapFont();
      // http://code.google.com/p/libgdx-users/wiki/ScreenAndGameClasses
     // TODO: use the same screen
     this.setScreen(new MainMenuScreen(this));
   }
   
   // render
  public void render() {
      super.render();  
   }
   
   // dispose
  public void dispose() {
      batch.dispose();
      font.dispose();
   }
   
}



I hope this shed some more light on the problem.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #5 - Posted 2013-09-16 08:27:04 »

I know this may be a stupid question, but i don't see you initializing your arraylist in any of your code. I have no idea if you just omitted this code, but this is really the only reason you should be getting a NPE.

Offline CodeHead

JGO Knight


Medals: 41


From rags to riches...to rags.


« Reply #6 - Posted 2013-09-16 08:36:38 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public class TextFill {
   
   // declarations
  public ArrayList<String> text = new ArrayList<String>();
   private Dealer game;
   
   // constructor
  public void fill() {
     
      // fill the array
     game.text.add("Michael?");
      game.text.add("Michael?!");
      game.text.add("Michael...?");
     
      //TODO: add the rest of the text
  }
}


Are you initializing the TextFill's member game object anywhere? If game has an ArrayList of it's own, why do you have the "text" member variable in the TextFill class that's never used? It looks like you're just writing your strings to a non existent object while ignoring the class' ArrayList altogether. This would definitely cause the list to be empty after construction. Your original approach sounds like the approach you should have gone with.

Arthur: Are all men from the future loud-mouthed braggarts?
Ash: Nope. Just me baby...Just me.
Offline sipty

Junior Newbie





« Reply #7 - Posted 2013-09-16 08:45:06 »

I know this may be a stupid question, but i don't see you initializing your arraylist in any of your code. I have no idea if you just omitted this code, but this is really the only reason you should be getting a NPE.

By initializing the arraylist, you mean this, I assume?
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
// declarations
  public ArrayList<String> text;
   
// constructor
  public void fill() {
      text = new ArrayList<String>();
     
      // fill the array
     text.add("Michael?");

// which is then called from a different class via
     
         // declarations
  private TextFill tf;
       
        // constructor
       tf = new TextFill();

        // render method
       game.batch.begin();
   game.font.draw(game.batch, tf.text.get(0), 350, 100);
        game.batch.end();


The array in the game object was one of the approaches I took later, but trying to fill it from anything else, but itself, had to results.

I do appologise, if I'm being not clear enough - first time I get to ask such a question through a forum ^^.
Offline SHC
« Reply #8 - Posted 2013-09-16 09:06:21 »

The basic difference between initialization and declaration is

1  
2  
3  
4  
5  
// Declaration
public ArrayList<String> text;

// Initialization
text = new ArrayList<String<>();

Hope you do not confuse..

Offline sipty

Junior Newbie





« Reply #9 - Posted 2013-09-16 09:11:22 »

Thank you for clarifying the wording for me!

I can now safely respond to opiop65 and CodeHead that - yes, I have initialized the array.

In fact, the latest code I posted was the way I first tried doing the array, alas I still got an NPE.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline sipty

Junior Newbie





« Reply #10 - Posted 2013-09-16 09:24:57 »

I just tried something new - declared and initialized the arraylist in the main class, called textfill, which filled the array and then tried accessing it through the gamescreen, which resulted in the following error:

Exception in thread "LWJGL Application" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

I have stumbled over this one earlier as well, when the arraylist was in the gamescreen class.

I tried adding one line in the main class and then adding three more in textfill, which just changed the error to: Index:0, Size:0.


I am not very sure what the issue is, to be completely honest. Maybe it has something to do with destroy? Although the documentation does contradict that suspicion...
Offline SHC
« Reply #11 - Posted 2013-09-16 09:25:16 »

Your code is lot confusing. We have some code formatting styles. If you are on eclipse, click on the menus
Source -> Format
That should format the code so we can read it without confusing. Also there are some coding conventions which you are not following. Here are some.

  • Class names should start with Capital letters
  • Method names should start with small letters
  • Declare all the class level variables at one place - usually before the constructor
  • Always try to code a better OOP design

And whenever you are showing your code to someone to get help, please post an
SSCCE (Short, Self Contained, Correct (Compilable), Example)
of your code so others can easily understand what you are trying to do.

If you are explaining something, try to format the content well but not too much so that others can read it clearly. Always double check before you post - There maybe some errors or you may phrase it in a wrong way that leads to assumptions by the reader. And if you are having any errors, please post the stacktrace so that we can easily find the error.

Good luck.

Offline sipty

Junior Newbie





« Reply #12 - Posted 2013-09-16 09:34:13 »

Thank you SHC, though I'm afraid I already follow all these rules, from naming to rereading my post 3times over and everything else you did mention. :-)

Now, the fact that I am trimming my code a bunch is because I am considering the problem to be theoretical and not really tied to my code.

Thank you for your time though, I do appreciate the effort! <3

Offline sipty

Junior Newbie





« Reply #13 - Posted 2013-09-16 10:21:14 »

Just fixed it. Smiley

I called the textFill screen, filled the array in the main game class, and then quickly moved on to the mainmenuscreen. Smiley

Thanks everyone for your time!


Best Regards
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.

TehJavaDev (18 views)
2014-08-28 18:26:30

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

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

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

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

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

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

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

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

BurntPizza (37 views)
2014-08-08 02:01:56
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!