shohamh
shohamh

Reputation: 317

Java - copy constructor - java.lang.NullPointerException

i'm trying to copy an object with a copy constructor, but it outputs an error:

Exception in thread "main" java.lang.NullPointerException
at Polynomial.<init>(Polynomial.java:30)
at Polynomial.showDerivative(Polynomial.java:59)
at Program.main(Program.java:9)

This is my copy constructor:

public Polynomial(Polynomial poly)
{
    for(int i = 0; i < a.length; i++)
        a[i] = poly.a[i];
    for(int i = 0; i < b.length; i++)
        b[i] = poly.b[i];
}

And this is how I instantiate the object:

Polynomial pol = new Polynomial(this);

What do I do?

Thanks.

Upvotes: 0

Views: 6565

Answers (3)

Rohit Jain
Rohit Jain

Reputation: 213311

You would be better with using System.arraycopy for creating a copy of your array.

Further, change your copy-constructor to: -

public Polynomial(Polynomial poly)
{
    int aLen = poly.a.length;
    int bLen = poly.b.length;

    // Initialize arrays for this object
    a = new int[aLen];  // Assuming `a` and `b` are integer arrays
    b = new int[bLen];  // Change accordingly.

    // A better way to create copy of arrays would be to use `System.arraycopy
    System.arraycopy( poly.a, 0, a, 0, aLen);
    System.arraycopy( poly.b, 0, b, 0, bLen);


    /*** You can avoid using below loops ***/

    // Iterate till the `aLen` of `poly` object passed 
    // and add elements to `a` array of this object
    /*for(int i = 0; i < aLen; i++)
        a[i] = poly.a[i];

    // Iterate till the `bLen` of `poly` object passed 
    // and add elements to `b` array of this object
    for(int i = 0; i < bLen; i++)
        b[i] = poly.b[i]; */
}

Your for loop should run till the length of poly.a and poly.b and not a and b, because they are not yet initialized, and hence NPE.

Upvotes: 1

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64066

You could just use:

public Polynomial(Polynomial poly) {
    a=poly.a.clone(); 
    b=poly.b.clone();
    }

which will create and copy the arrays in one step each.

Upvotes: 0

andrewdotn
andrewdotn

Reputation: 34873

Assuming your Polynomial class looks like this:

public class Polynomial {

    private int[] a;
    private int[] b;

    public Polynomial(int length) {
        a = new int[length];
        b = new int[length];
    }

    public Polynomial(Polynomial poly)
    {
        for(int i = 0; i < a.length; i++)
            a[i] = poly.a[i];
        for(int i = 0; i < b.length; i++)
            b[i] = poly.b[i];
    }


    public static void main(String[] args) {
        Polynomial p = new Polynomial(2);
        Polynomial q = new Polynomial(p);
    }

}

Then the problem is that the instance variables a and b aren’t initialized in the copy constructor. Each constructor is independent, and if you want to perform actions from one inside another, you have to do it explicitly by calling a common initialization function, e.g.

public class Polynomial {

    private int[] a;
    private int[] b;

    private void init(int length) {
        a = new int[length];
        b = new int[length];
    }

    public Polynomial(int length) {
        init(length);
    }

    public Polynomial(Polynomial poly)
    {
        init(poly.a.length);
        for(int i = 0; i < a.length; i++)
            a[i] = poly.a[i];
        for(int i = 0; i < b.length; i++)
            b[i] = poly.b[i];
    }


    public static void main(String[] args) {
        Polynomial p = new Polynomial(2);
        Polynomial q = new Polynomial(p);
    }

}

Upvotes: 0

Related Questions