Results 1 to 7 of 7
  1. #1
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default 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;
        }
    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é

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

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

  4. #4
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    The new keyword makes a new instance in the method. Thats why.

  5. #5
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  6. #6
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    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.

  7. #7
    aborgeld is offline Senior Member
    Join Date
    Dec 2010
    Location
    The Hague
    Posts
    114
    Rep Power
    0

    Default

    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

  1. XML and SAX - class returns 'null'
    By Laffel in forum Advanced Java
    Replies: 2
    Last Post: 03-07-2011, 10:14 PM
  2. Splashscreen returns null
    By Charlie161 in forum AWT / Swing
    Replies: 2
    Last Post: 03-04-2011, 02:25 PM
  3. getImplementationVersion() returns null
    By newbiejava in forum New To Java
    Replies: 22
    Last Post: 09-12-2010, 10:31 AM
  4. Replies: 1
    Last Post: 08-20-2010, 12:30 PM
  5. helpset findHelpSet returns null
    By kmm1977 in forum AWT / Swing
    Replies: 23
    Last Post: 06-22-2010, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •