2

I have a game with some asteroid objects in an arraylist. To keep my code neat and tighty-ish, I want to pass this arraylist to different classes/methods. I thought I had it figured out.. but apparently not. Here goes: My Level1 class starts by an initializing method (init) that calls the populateAsteroid method in the asteroid class. The Level1 class furthermore contains an update function that loops through the asteroid arraylist and updates it. All of this works fine:

public class Level1 {   
  private Asteroid populateAsteroid
  private Collision collision;

  public void init() {

    populateAsteroid = new Asteroid();
    // create and populate an arraylist with asteroid objects
    populateAsteroid.populateAsteroid(1);   
    collision = new Collision();
  } 

  public void update() {
    // get the arraylist
    ArrayList<Asteroid> asteroidList = populateAsteroid.getAsteroidList();

    for(int i = 0; i < asteroidList.size(); i++) {           
        Asteroid a = asteroidList.get(i);
        a.update(i);
    }
    collision.checkCollision();
  }
}


public class Asteroid  {    
  public ArrayList<Asteroid> asteroidList;

  public void populateAsteroid(int level) {
    // if the arraylist is null, as when the function is called by init, create the array
    if (asteroidList == null){
        asteroidList = new ArrayList<Asteroid>();
    }

    for (int i = 1 + asteroidList.size(); i <= (asteroidsAtEachLevel[level]); i++){
        Asteroid a = new Asteroid();
        asteroidList.add(a);
    }
  }

    public ArrayList<Asteroid> getAsteroidList() {
       return asteroidList;
    }   
}

However, the trouble comes in my collision class, called by the update function in the Level1 class, which contains the checkCollision method

public class Collision {

  private Asteroid a;

  public void checkCollision(){

    a = new Asteroid();
    // get the arraylist                
    ArrayList<Asteroid> asteroidList = a.getAsteroidList();

    if (asteroidList == null){
        System.out.println("null");}
    }
    ..."more code"....

The list is null, even though I believe I am retrieving the arraylist identically to the way I do it in the Level1.update function. Is it because the arraylist is some how not visible as it is created from a call in the Level1 class? Any suggestions?

5
  • 1
    You never call populateAsteroid on a, so the list never gets created. Commented Sep 2, 2016 at 20:37
  • 1
    You never populate the asteroid, so its list is still null. Did you perhaps mean to use the one you created back in your init() method, instead creating a new (empty) one in checkCollision()? Commented Sep 2, 2016 at 20:37
  • You are saying a = new Asteroid(); and then calling a.getAsteroidList();, This is not initializing the asteroidlist Commented Sep 2, 2016 at 20:37
  • Well, either you make asteroidList static or you create another class, like AsteroidHandler which manages the list of asteroids. Then you need to pass that handler instance to checkCollision. Commented Sep 2, 2016 at 20:40
  • In Collision you're creating a new Asteroid object and then calling the getter method for the Asteroid List. The asteroid list associated with that brand new object is never set to anything, so it's null. Commented Sep 2, 2016 at 20:42

3 Answers 3

3

In checkCollision() you are creating a brand new Asteroid object. When a new instance of the Asteroid class is made, nothing will create the List in its constructor. This is why you see null.

For checkCollision() to work correctly, it needs to check the same Asteroid object that you populate earlier on.

You can achieve this by passing the Asteroid object you made earlier to it like

public void checkCollision(Asteroid a){              
    ArrayList<Asteroid> asteroidList = a.getAsteroidList();

    if (asteroidList == null){
        System.out.println("null");}
    }
    ..."more code"....
}

Then modify your init() method

public void init() {
    populateAsteroid = new Asteroid();
    populateAsteroid.populateAsteroid(1);   
    collision = new Collision();
    collision.checkCollision(populateAsteroid)
}
Sign up to request clarification or add additional context in comments.

2 Comments

That makes perfect sense, thank you. One of the reasons I want a collision class is that I have several array lists with different objects (comet, asteroid, enemies etc). How do I pass an arbitrary object to the check collision function? Earlier I had a check collision method in my comet class, asteroid class etc but I would like to have one method to cover it all. That is why I thought I could get the method to get the needed lists instead of passing it to the method when calling it.
Thinking about it - I guess I can just pass all the different array lists to the checkCollision method simultaneously and check them all at once instead of calling the checkCollision method individually for each array list.
2

You are not populating your list of asteroids. You are instantiating a new class, but not doing the required methods to populate the list:

a = new Asteroid();
a.populateAsteroid(someInteger)             
ArrayList<Asteroid> asteroidList = a.getAsteroidList();

When you create the Asteroid class object, you are only declaring the list. You aren't assigning it a value:

public ArrayList<Asteroid> asteroidList;

It isn't until the populateAsteroid method that a new instance of the ArrayList is created. If you leave it to the declaration, it will return null. That's why the following if-condition will always return true:

if (asteroidList == null){
    asteroidList = new ArrayList<Asteroid>();
}

Comments

1

You are creating a brand new Asteroid in your Collision class, one that is not initialized. You should instead pass in an instance of your previously initialized asteroid into the class.

For example:

public class Level1 {

    private Asteroid populateAsteroid
    private Collision collision;

    public void init() {

        populateAsteroid = new Asteroid();
        // create and populate an arraylist with asteroid objects
        populateAsteroid.populateAsteroid(1);   
        collision = new Collision(populateAsteroid); //Pass in the reference to the asteroid.

}

and

public class Collision {

private Asteroid a;

public void checkCollision(Asteroid rock){

    a = rock;
    // get the arraylist                
    ArrayList<Asteroid> asteroidList = a.getAsteroidList();

    if (asteroidList == null){
        System.out.println("null");}
    }
    ..."more code"....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.