繁体   English   中英

奇怪的空指针异常

[英]weird Null Pointer Exception

我正在尝试使用名为stack和buffer的2个堆栈来实现一个数组。 最初堆栈充满随机值,MyArrayI接口包含2个函数:getItemAt(int index)和setItemAt(int index,int item)。 它们在MyStackArray类中成功实现。 每当我运行程序时,我得到Null异常错误,我试图追踪原因,直到我发现堆栈没有填充初始数据 - 也许。 每当我尝试访问堆栈或缓冲区时,我都会收到NullPointerException错误。 当我尝试打印数组堆栈中的元素时,我仍然得到了愚蠢的NPE错误!

public class MyStackArray implements MyArrayI {
    private int[] buffer;
    private int[] stack;
    private int maxSize;

    public MyStackArray(int s){
        maxSize = s;
        int[] buffer = new int [maxSize];
        int[] stack = new int [maxSize];

        for(int i=0; i<maxSize; i++)
            stack[i]=i;    // initiallizing the array with random values.
    }

    public void print(){              // tried to check the contents of the stack array.
         for(int i=0; i<maxSize; i++)
          System.out.println(stack[i]); // causes Null Pointer Exception. ??!
        //System.out.println(stack[0]); // still causes the NPE !! 
    }

    public void setItemAt(int index, int item){
        int i;
        int j;

            for(i=0, j=maxSize-1 ; i<maxSize && j>=0 ; i++, j--){
                if(j == index)
                    break;
                buffer[i] = stack[j];  //causes a NULL.PointerException
           }
           stack[j] = item;   
    }

    public int getItemAt(int index){
        int i;
        int j;

            for(i=0, j=maxSize-1 ; i<maxSize && j>=0; i++, j--){
                if(j==index)
                    break;
                buffer[i] = stack[j];   // causes NPE
           }
           return stack[j];  
    }

    public static void main(String[] args) {

      MyStackArray X = new MyStackArray(3);
      //X.setItemAt(0,4);  // causes NPE
      //X.getItemAt(1);    // causes NPE
    X.print();             // causes NPE
    }
}
int[] stack = new int [maxSize];

在这里,您正在创建一个名为stack的新变量 - 这与this.stack 你想要:

stack = new int[maxSize];  // i.e. initialize this.stack, not another variable

当未初始化时, this.stack保持为null ,当您尝试访问它时,您会收到NPE。

PS你也用buffer做同样的事情。

您没有正确初始化变量:

public MyStackArray(int s){
    maxSize = s;
    int[] buffer = new int [maxSize]; // Initializes LOCAL buffer
    int[] stack = new int [maxSize]; // Initializes LOCAL stack

    for(int i=0; i<maxSize; i++)
        stack[i]=i;    // initiallizing the array with random values.
}

改变如下:

public MyStackArray(int s){
    maxSize = s;
    buffer = new int [maxSize];
    stack = new int [maxSize];

    for(int i=0; i<maxSize; i++)
        stack[i]=i;    // initiallizing the array with random values.
}

您实际上是在构造函数中初始化新(本地)数组。 你有

public MyStackArray(int s){
    maxSize = s;
    int[] buffer = new int [maxSize];  //You are declaring new array
    int[] stack = new int [maxSize];  //You are declaring new array

    for(int i=0; i<maxSize; i++)
        stack[i]=i;    // initiallizing the array with random values.
}

但是你应该在构造函数中使用它:

public MyStackArray(int s){
    maxSize = s;
    buffer = new int [maxSize];
    stack = new int [maxSize];

    for(int i=0; i<maxSize; i++)
        stack[i]=i;    // initiallizing the array with random values.
}

暂无
暂无

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

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