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);
5
  • 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 Commented Dec 2, 2015 at 14:54
  • 2
    Possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented Dec 2, 2015 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);? Commented Dec 2, 2015 at 14:58
  • @phflack that also had to be fixed, thank you. Commented Dec 2, 2015 at 15:00
  • @showp1984 It is supposed to be. Had to fix that too, it didn't work otherwise! Commented Dec 2, 2015 at 15:01

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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.

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.