1

I can't seem to figure this out and if you guys could help me that would be awesome! I am trying to pass already made objects into a constructor so I can get all of their values.

public class Drops {
  Ship ship;
  Bullet[] bullet;
  Aliens[] aliens;
  Movement movement;

  public Drops(Ship ship,Bullet[] bull,Aliens[] alienT) {
    this.ship = ship;
    for (int a = 0; a < MainGamePanel.maxAliens;a++) {
      System.out.println(a +" " +alienT[a].x); // THIS WORKS, when nothing
                                               // is being assigned, so the values 
                                               // are being passed correctly.
      this.aliens[a] = alienT[a];
      for (int b = 0; b < MainGamePanel.maxShots;b++){
        this.bullet[b] = bull[b];
  }
    }
  }
// that is is the class, and also where the error occurs

in the main I am sending the values to the constructor like this

drop = new Drops(ship, bull, alienT);

ship is not an array bull and alienT are both arrays.

Thank you in advance!

3 Answers 3

1

You need to initialize the arrays:

Bullet[] bullet;
Aliens[] aliens;

e.g:

public Drops(Ship ship,Bullet[] bull,Aliens[] alienT){
    this.ship = ship;
    this.bullet = new Bullet[bull.length];
    this.aliens = new Aliens[alianT.length];
    // ..

Also, make sure that the loop condition takes into account the length of alienT and bull, if they are shorter than MainGamePanel.maxAliens and MainGamePanel.maxShots you'll get ArrayIndexOutOfBoundsException.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, that worked. It's funny because I knew that and was messing around with that but failed miserably and put them in the damn loops! edit: And yeah maxAliens and maxShots are actually declaring the other objects array size when they get initialized in MainGamePanel.
0

You can define bull and allienT parameters as Collection<Bullet> and Collection<AllienT> respectivelly.

Then you can call this method passing an ArrayList, HashSet or your prefered collection class.

2 Comments

That went a little over my head haha, I am still very new with java and programming in general, learning quickly though. A collection I am assuming is another way to just use the array called from the constructor without making new ones? Is it usable in other methods in the same class?
Collection is an Interface implemented by many Data Structure classes like ArrayList, HashSet, LinkedHashSet, TreeSet... You can define parameters as Collections and you can work with the implementation you decide for each case. Here is some documentation: [link] docs.oracle.com/javase/6/docs/api/java/util/Collection.html
0

You're getting the NPE because of the aliens and bullet member arrays are null. Make sure you're instantiating them in the constructor with the proper length:

public Drops(Ship ship,Bullet[] bull,Aliens[] alienT){
    this.ship = ship;
    this.aliens = new Aliens[alienT.length];
    this.bullet = new Bullet[bull.length];
    // ...
}

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.