-5

I have the following Java class that adds a Person object to an existing Person Array:

 public class PersonService{

 protected int lastItemInPersonArray = 0;

 private Person[] persons = new Person[100];


 public void addPersonToPersonArray(Person personToAdd){

        persons[lastItemInPersonArray++] = personToAdd;

    }

}

I can add 1 object correctly here but when I try to 2 I get the following error:

java.lang.ArrayIndexOutOfBoundsException: 1

What is incorrect with my logic that is causing this?

java123999
  • 6,974
  • 36
  • 77
  • 121
  • Have you tried to make `protected int lastItemInPersonArray` private? It seems that you are changing this variable somewhere. – Yuri H Jun 01 '17 at 10:38
  • I am not changing the value of this int anywhere expect in the method shown above. – java123999 Jun 01 '17 at 10:39
  • Can you show the full stack trace and class Person – Yuri H Jun 01 '17 at 10:39
  • 1
    Well you declare a `Person[] persons = new Person[100];` of 100 cells. But you get an exception with index 1. Check the length of your array. It might be initialize somewhere else. But of course, you should add a condition there to block the insertion when you reach the end. You should see `java.util.List` if you want "unlimited" array. Has for now, we need a [mcve] as this is not reproducible with only 3 `addPersonnToPersonArray` call – AxelH Jun 01 '17 at 10:40
  • 1
    How do you execute this code? It looks fine to me, and it works if I run it (with a dummy Person class). – Riaan Nel Jun 01 '17 at 10:40
  • 2
    I also think you change the `lastItemInPersonArray`,you can print the `lastItemInPersonArray` and the `persons` array length in the `addPersonToPersonArray` method – dabaicai Jun 01 '17 at 10:40
  • are you doing the operation in this class or in it's subclass? – Amer Qarabsa Jun 01 '17 at 10:42
  • As this code is currently presented, I find no way that the Exception you mentioned can be generated. Please post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – dumbPotato21 Jun 01 '17 at 10:43
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Jaroslaw Pawlak Jun 01 '17 at 11:31

1 Answers1

1

This works for me.

public class PersonService {
    protected int lastItemInPersonArray = 0;
    private Person[] persons = new Person[100];
    public void addPersonToPersonArray(Person personToAdd) {
        persons[lastItemInPersonArray++] = personToAdd;
    }   
    public static void main(String[] args) {
        PersonService ps = new PersonService();
        ps.addPersonToPersonArray(new Person("P 1"));
        ps.addPersonToPersonArray(new Person("P 2"));
        ps.addPersonToPersonArray(new Person("P 3"));   
        System.out.println(ps.persons[0].nome);
        System.out.println(ps.persons[1].nome);
        System.out.println(ps.persons[2].nome);
    }
}
class Person{
    public Person(String nome) {
        this.nome = nome;
    }
    String nome;
}
Luiz Lanza
  • 154
  • 1
  • 3