Results 1 to 7 of 7
Thread: ArrayList returns null
- 04-01-2011, 11:16 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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:
Java 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; }
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é
- 04-01-2011, 11:48 AM #2
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
Java Code:return [B]this[/B].playgroundStringList;
Skype: petrarsentev
http://TrackStudio.com
- 04-01-2011, 12:57 PM #3
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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..
- 04-01-2011, 01:01 PM #4
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
The new keyword makes a new instance in the method. Thats why.
- 04-01-2011, 01:03 PM #5
Sorry It's difficult to understand for me. You can use forEach loop for it.
What is "spelActief.getPlaygroundAantal()" there?Skype: petrarsentev
http://TrackStudio.com
- 04-01-2011, 01:07 PM #6
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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.
- 04-01-2011, 02:32 PM #7
Senior Member
- Join Date
- Dec 2010
- Location
- The Hague
- Posts
- 114
- Rep Power
- 0
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.
Java 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; }
Java Code:playgroundComboBox = new JComboBox(spelHandler.getPlayground(spelActief));
Similar Threads
-
XML and SAX - class returns 'null'
By Laffel in forum Advanced JavaReplies: 2Last Post: 03-07-2011, 10:14 PM -
Splashscreen returns null
By Charlie161 in forum AWT / SwingReplies: 2Last Post: 03-04-2011, 02:25 PM -
getImplementationVersion() returns null
By newbiejava in forum New To JavaReplies: 22Last Post: 09-12-2010, 10:31 AM -
Lucene highlighter returns null fragments always
By zee in forum LuceneReplies: 1Last Post: 08-20-2010, 12:30 PM -
helpset findHelpSet returns null
By kmm1977 in forum AWT / SwingReplies: 23Last Post: 06-22-2010, 04:53 PM
Bookmarks