0

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.

10
  • 3
    Post your Polynomial class, not just the constructor. We need to know, what is a? Commented Jan 22, 2013 at 18:55
  • 1
    Check the length of poly.a and poly.b and don't use it blindly in these lines a[i] = poly.a[i]; and b[i] = poly.b[i];. Commented Jan 22, 2013 at 18:56
  • Why do i need to check them? They're supposed to be exactly the same. Commented Jan 22, 2013 at 18:57
  • 2
    Thanks, managed it because of Srinivas. Commented Jan 22, 2013 at 19:01
  • 1
    My vote would be for a=poly.a.clone(); b=poly.b.clone();. Commented Jan 22, 2013 at 19:04

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yea, that's what I did, I'll tick it in 5 minutes when it'll let me.
0

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);
    }

}

Comments

0

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.

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.