[SOLVED][libGDX] Dedicating a class to fill an array, while implementing screens

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!

Very very vague question, need specific code.

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.

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.

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:

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.



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.


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.

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.

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.

By initializing the arraylist, you mean this, I assume?


// 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 ^^.

The basic difference between initialization and declaration is


// Declaration
public ArrayList<String> text;

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

Hope you do not confuse…

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.

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…

Your code is lot confusing. We have some code formatting styles. If you are on eclipse, click on the menus [icode]Source -> Format[/icode] 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 [icode]SSCCE (Short, Self Contained, Correct (Compilable), Example)[/icode] 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.

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. :slight_smile:

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

Just fixed it. :slight_smile:

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

Thanks everyone for your time!

Best Regards