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.
You would be better with using
System.arraycopyfor creating a copy of your array.Further, change your copy-constructor to: –
Your
forloop should run till the length ofpoly.aandpoly.band notaandb, because they are not yet initialized, and henceNPE.