0

I know this is usually because something hasn't been initialised, but I've checked and I'm sure everything's been initialised! I've looked at loads of examples to check how to initialise arrays of objects. Exact error:

Exception in thread "main" java.lang.NullPointerException

at java.lang.System.arraycopy(Native Method)

at SolarSim.copyArray(solarSim.java:17)

at SolarSim.main(Solarsim.java:117)

So I think that's pointing to my copyArray method, and the lines where I've used it, so here are those bits of code:

public static PhysicsVector[] copyArray(PhysicsVector[] a) {
    int length = a.length;
    PhysicsVector[] copy = new PhysicsVector[length];
    for (int i = 0; i < length; i++) {
        System.arraycopy(a[i], 0, copy[i], 0, length);
    }
    return copy;
}

And another section that uses the copyArray method:

    GravField[] gravityObject = {earthGravField, sunGravField};
    PhysicsVector[] position = {newPos, sunNewPos}; 
    PhysicsVector[] velocity = {newV, sunNewV};
    PhysicsVector[] gravField = {earthGrav, sunGrav};
    double[] planetMass = {earthMass, sunMass};
    Particle[] planets = {earth, sun};

    PhysicsVector[] newP= new PhysicsVector[position.length];

    PhysicsVector[] newGrav = new PhysicsVector[gravField.length];

    PhysicsVector[] newVel = new PhysicsVector[velocity.length];

    PhysicsVector sum1 = new PhysicsVector(0,0);
    PhysicsVector sum2= new PhysicsVector(0,0);
    PhysicsVector sum3= new PhysicsVector(0,0);

    do{
        PhysicsVector[] y = new PhysicsVector[gravField.length];
        y=copyArray(gravField);                        //This is line 117, pointed to in error message
        PhysicsVector[] z = new PhysicsVector[gravField.length];
        z=copyArray(gravField);
RockAndRoll
  • 2,247
  • 2
  • 16
  • 35
user13948
  • 443
  • 1
  • 6
  • 14
  • I'm kind of surprised `System.arraycopy(PhysicsVector, int, PhysicsVector, int, int);` even works, I was under the impression it required arrays. Then again that's probably why you're getting errors – phflack Dec 02 '15 at 14:54
  • 2
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – ΦXocę 웃 Пepeúpa ツ Dec 02 '15 at 14:55
  • 1
    I may be wrong, but isn't `System.arraycopy(a[i], 0, copy[i], 0, length);` supposed to be used like this `System.arraycopy(a, i, copy, i, length);`? – showp1984 Dec 02 '15 at 14:58
  • @phflack that also had to be fixed, thank you. – user13948 Dec 02 '15 at 15:00
  • @showp1984 It is supposed to be. Had to fix that too, it didn't work otherwise! – user13948 Dec 02 '15 at 15:01

2 Answers2

3

You pass array elements and not the array to System.arraycopy. The NullPointerException is because copy[i] is null.

The right way is to pass the arrays, no need to loop over the elements:

public static PhysicsVector[] copyArray(PhysicsVector[] a) {
    int length = a.length;
    PhysicsVector[] copy = new PhysicsVector[length];
    System.arraycopy(a, 0, copy, 0, length);
    return copy;
}

In case you want a shallow copy of an array it is even easier:

public static PhysicsVector[] copyArray(PhysicsVector[] a) {
    return a.clone();
}
wero
  • 32,544
  • 3
  • 59
  • 84
1

You don't need to have the Sytsem.arraycopy call in a for loop. Just call it once and the entirety of the source array will be copied to the target array.

sgabhart22
  • 38
  • 5