0

I am trying to copy a new array at the end of the one i have, but it keeps saying ArrayStoreException in the System.arraycopy and i dont know why, it should have space and everything.

this is the code:

objectzombie=zombieParser(result); /*give a 4 object array*/
GameObjectList arrayt= new GameObjectList(objectzombie.size()+this.objectlist.size());

System.arraycopy(objectzombie, 0, arrayt, 0, objectzombie.size());
System.arraycopy(this.objectlist, 0, arrayt, arrayt.size(), 
this.objectlist.size());
this.objectlist=arrayt;}

thank you for your help;

EDIT-------------

It looks like, i can't use the arraycopy if i don´t have a primitive array, how can i then combine my two lists? i don't know how could i do it.

eisklat
  • 41
  • 6
  • 1
    please show the relevant code and the relevant stacktrace, you are showing way too little information – Stultuske Dec 05 '18 at 12:38
  • According to documentation: `Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects` – Amongalen Dec 05 '18 at 12:46
  • Possible duplicate of [ArrayStoreException in java arrays](https://stackoverflow.com/questions/24207155/arraystoreexception-in-java-arrays) – rghome Dec 05 '18 at 12:49

1 Answers1

1

According to Java SE API documentation, Arguments of System.arraycopy are:

  • src - the source array.
  • srcPos - starting position in the source array.
  • dest - the destination array.
  • destPos - starting position in the destination data.
  • length - the number of array elements to be copied.

In the method description, there is also:

Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

  • The src argument refers to an object that is not an array.

  • The dest argument refers to an object that is not an array.

  • The src argument and dest argument refer to arrays whose component types are different primitive types.

  • The src argument refers to an array with a primitive component type and the dest argument refers to an array with a reference component type.

  • The src argument refers to an array with a reference component type and the dest argument refers to an array with a primitive component type.

the src and dest are array, whereas you tried to copy to target object of type GameObjectList, which is a mismatch => ArrayStoreException

Community
  • 1
  • 1
Rui
  • 3,454
  • 6
  • 37
  • 70
  • I see, so i guess i can't use this. I will try to find another way then... but i cant think about one. – eisklat Dec 05 '18 at 14:30
  • I modified the answer more clearly just now. You can read again to get to know how to use the core Java API :) – Rui Dec 05 '18 at 14:44
  • Thank you, but i nees to copy the arraylist one, so i guess i will have to use another thing – eisklat Dec 05 '18 at 19:59