简体   繁体   English

Java-复制构造函数-java.lang.NullPointerException

[英]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. 谢谢。

You would be better with using System.arraycopy for creating a copy of your array. 使用System.arraycopy创建阵列副本会更好。

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 . for循环应该运行,直到长度poly.apoly.b ,而不是ab ,因为他们还没有初始化,因此NPE

Assuming your Polynomial class looks like this: 假设您的Polynomial类如下所示:

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. 那么问题在于实例变量ab没有在复制构造函数中初始化。 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, eg 每个构造函数都是独立的,如果要从另一个内部执行动作,则必须通过调用一个通用的初始化函数来显式地进行操作,例如

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

}

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. 每一步将创建和复制阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM