title: Java - how to call an add() method in a different class
body: <p>This is for homework, and I am becoming a little frustrated with how I can't figure out something so simple. </p>
  
  <p>To simplify my code, I have 3 files right now: one class with an add() method I created among other things, one file that tests it (made by the prof), and one that creates the object (which I won't post, b/c its working). Here's the add() function.</p>
  
  <p><strong>EDIT 2:</strong> I'm going to add the method that prints the array, maybe that's the problem?</p>
  
  <pre><code>    public class Population {
        private Person[] pop = new Person[15];
        private int numPop = 0;    
  
        public void add(Person c){ // this object is created in another class, it works fine
          for(int i = 0; i &lt; pop.length; i++){
            if(pop[i] == null) {
              pop[i] = c;
              numPop++;
            } else {}
          }
  
   public String listPeople(){
        System.out.println("Population with "+numPeople+" people as follows:");
        int i = 0;
        while (i&lt;numPeople){
         System.out.println("A "+pop[i].getAge()+"year old person named "+pop[i].getName());
          i++;
  //FYI the get methods are working fine and are in another file.
   } 
        return("");
        }
  </code></pre>
  
  <p>Then, I run the program in a test file to ensure it works, which was provided to us. Here's the part that isn't working</p>
  
  <pre><code>public class PopTestProgram{ // FYI the prof created this, I can't change this
    public static void main(String[] args){
  
      Population pop = new Population(15);
  
      pop.add(new Person(4, "Bob"));
      pop.add(new Person(25, "Kim"));
      // then adds 8 more people with different ages and names
      // then prints the people
  </code></pre>
  
  <p>It compiles, but when I run it, it just puts 10 of the last person into the array, then crashes saying there is a problem with the <code>"pop[i] = c;"</code> line. I simply cannot figure out what I need to change here.</p>
  
  <p>I haven't received an email from the prof directly, so I thought I'd ask here.</p>
  
  <p>Edit: Here's what it shows after printing out the last person 10 times. It is showing problems with other methods that I haven't completed yet though...</p>
  
  <pre><code>java.lang.ArrayIndexOutOfBoundsException: -1
      at Population.removePerson(Population.java:49)
      at PopTestProgram.main(PopTestProgram.java:31)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
      at java.lang.reflect.Method.invoke(Unknown Source)
      at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
  </code></pre>