Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 调用构造函数时出现nullpointer异常_Java - Fatal编程技术网

Java 调用构造函数时出现nullpointer异常

Java 调用构造函数时出现nullpointer异常,java,Java,每当我尝试调用任何构造函数时,都会得到一个null指针执行选项 public class Poly{ private static class Pair{ int coeff; int exponent; } int count=1; private Pair[] poly =new Pair[count]; public Poly(){ poly = new Pair[count]; poly[count-1].coeff=0; poly[count-1]

每当我尝试调用任何构造函数时,都会得到一个null指针执行选项

public class Poly{

private static class Pair{

int coeff;
int exponent;

}

int count=1;

private Pair[] poly =new Pair[count];

public Poly(){

    poly = new Pair[count];
    poly[count-1].coeff=0;
    poly[count-1].exponent=0;
    count++;
}
public Poly(int coeff1, int exp){

    poly = new Pair[count];
    //poly.add(new Poly(coeff1, exp));
    poly[count-1].coeff=coeff1;
    poly[count-1].exponent=exp;
    count++;
}

private Poly (int exp) {

       poly = new Pair[exp+1];
       poly[0].exponent = exp;
    }





public String toString(){

    String str ="";
    for(int i=0; i<=count; i++){
        str+= poly[i].coeff +"x^" +poly[i].exponent;
    }
    return str;
}

public static void main(String[] args) {

        Poly p =new Poly(5, 3);
        System.out.println(p.toString());
    }


}
公共类Poly{
私有静态类对{
内因系数;
整数指数;
}
整数计数=1;
私有对[]多边形=新对[计数];
公共保利(){
poly=新对[计数];
多边形[count-1]。系数=0;
多边形[count-1]。指数=0;
计数++;
}
公共多边形(int coeff1,int exp){
poly=新对[计数];
//poly.add(新的poly(coeff1,exp));
poly[count-1].coeff=coeff1;
多边形[count-1]。指数=exp;
计数++;
}
私有多边形(int exp){
poly=新对[exp+1];
多边形[0]。指数=exp;
}
公共字符串toString(){
字符串str=“”;

对于(inti=0;i来说,仅仅实例化数组本身是不够的,还必须实例化数组的元素

public Poly(){

    poly = new Pair[count];
    poly[count-1] = new Pair(/*params*/);
    poly[count-1].coeff=0;
    poly[count-1].exponent=0;
    count++;
}

创建新的对象数组时,它们默认为null,因此

Pair poly = new Pair[10];
poly[0] == null // true
poly[9] == null // true
您需要初始化数组

public Poly(){

    poly = new Pair[count];
            for(int i=0;i<count;i++) 
                poly[i] = new Pair();
    poly[count-1].coeff=0;
    poly[count-1].exponent=0;
    count++;
}
public Poly(){
poly=新对[计数];

对于(inti=0;i,创建一个大小为count元素的数组对象

数组中的每个元素都是null,所以

poly[count-1].coeff=0; 
将使用空指针

您必须在多边形构造器中创建对:

for(int i =0;i<count;i++){
    poly[i] = new Pair();
}
for(int i=0;i此代码:

poly = new Pair[count];
从不调用构造函数,因此下一行

poly[count-1].coeff=0;
…因NPE而失败

第一行所做的就是创建一个
null
引用数组,您还没有创建任何
Pair
对象。要实际创建
Pair
对象,您必须执行以下操作:

poly = new Pair[count];                    // Not calling the constructor
for (int i = 0; i < poly.length; ++i) {
    poly[i] = new Pair(/*...args...*/);    // Calling the constructor
}
poly[count-1].coeff=0;
poly=newpair[count];//不调用构造函数
对于(int i=0;i