简体   繁体   中英

weird Null Pointer Exception

I am trying to implement an array using 2 stacks named stack and buffer. Initially the stack is filled with random values, the MyArrayI interface contains 2 functions: getItemAt(int index) and setItemAt(int index, int item). and they are successfully implemented in the MyStackArray class. whenever I run the program I get Null exception error, I have tried to trace the cause till I found that the stack isn't filled with the initial data - maybe. whenever I try to access stack or buffer I get the NullPointerException Error. and when I try to print the elements inside the array stack, I still get the silly NPE Error !!!

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];

Here, you are creating a new variable called stack - this is not the same as this.stack . You instead want:

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

When left uninitialized, this.stack remains null and you receive an NPE when you try to access it.

PS You're also doing the same thing with buffer .

You aren't properly initializing your variables:

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.
}

Change like this:

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.
}

You are actually initializing new (local) arrays in your constructors. you have

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.
}

But you should instead have this in your constructor:

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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