-
ArrayList returns null
I want to use this method for filling my combobox, i want to store information in an Arraylist, but this returns null, wheni print the method:
Code:
public ArrayList getPlaygroundStringList(Spel spelActief){
ArrayList <String> playgroundStringList = new ArrayList();
for (int i = 0; i < spelActief.getPlaygroundAantal();i++)
playgroundStringList.add(spelActief.playground.getNaam());
return this.playgroundStringList;
}
1. The parameter works
2. when i print this (spelActief.playground.getNaam()) i get:
playground1
The purpose is to fill this arraylist when passing a playground.
playground1, playground2 and so on.
Why does it return null?
Kind regards,
André
-
It's easy. You have two variables. There are have the same name and you pass variable, which you are not fulling. Just delete key work "this" from there
Code:
return [B]this[/B].playgroundStringList;
-
Thanks, your right, it works.
The for loop gives me.
I made another mistake to add the for loop there.
result:
playground1, playground1, playground1, playground1
So i've changed that. So the result is:
playground1
And when i start in level two the result is:
playground2
It seems like the first value is gone.
How do i get it to save every value that the getMethod gives?
like this: playground1, playground2 etc..
-
The new keyword makes a new instance in the method. Thats why.
-
Sorry It's difficult to understand for me. You can use forEach loop for it.
What is "spelActief.getPlaygroundAantal()" there?
-
Not a problem.
That gives me the active playground.
So if i play in playground 1 it gives playground1, when i get to the next playground, it gives me playgroud2.
And i want to store everything it gives me after i played each playground in an ArrayList.
-
This is what i want (see code), i managed to do it now, but the code is not so flexibel.
spelActief.playground.getName()), gives me the playground i'm on at the moment, example: playground1.
spelActief.getPlaygroundAantal(); gives me the sixe of the playgroundList.
Code:
public String[] getPlayground(Spel spelActief){
String[] playGr1 = {"Playground 1"};
String[] playGr2 = {"Playground 1", "Playground 2"};
String[] playGr3 = {"Playground 1", "Playground 2", "Playground 3"};
String[] playGr4 = {"Playground 1", "Playground 2", "Playground 3", "Playground 4"};
if (spelActief.playground.getNaam().equals("playground1"))
return playGr1;
else if(spelActief.playground.getNaam().equals("playground2"))
return playGr2;
else if(spelActief.playground.getNaam().equals("playground3"))
return playGr3;
else
return playGr4;
}
Code:
playgroundComboBox = new JComboBox(spelHandler.getPlayground(spelActief));