algorithm - thread "main" java.lang.NullPointerException, Implement three stack in a single array -


the purpose of code implement 3 stacks in single array. use linked node implement stack. elements pushed array 1 one directly, , elements in each stack connected previous pointer. pointer int value corresponding index in array item stored. nextavaindexmethod return next available index can store new pushed item. because there space released in beginning of array after executing pop method. ifindexused < arr.lengthit keep moving forward store new item, while if indexusedreaches end of array, method search there free space in beginning of array. when run it, throws nullpointerexception, know meaning of error, can't fix it. comments! code correct? 1 more question of removal item int type array. letarr[i].data = 0 delete item, , use statement arr[i].data == 0 check if 1 space null. if 1 space store0? suggestion!

 public class flexiblemultistack {

private int[] toppoint = {-1, -1, -1};// assume number of stack ==3; private int indexused = 0; private stacknode[] arr;   public flexiblemultistack(int sizeeach, int stackno) {      arr = new stacknode[sizeeach * stackno]; //  }  public boolean isempty(int stacknum) {     return toppoint[stacknum] == 0; }  public void push(int item, int stacknum) {     int lastindex = toppoint[stacknum];     int nextindex = nextavaindex();     if (nextindex == -1) {       // if nextindex = -1, there no more space!         system.out.println("there no more space!");     } else {         toppoint[stacknum] = nextindex;         arr[toppoint[stacknum]] = new stacknode(item, lastindex);         indexused++;     } }  public int pop(int stacknum) {     if (toppoint[stacknum] == -1) {         return 0;     } else {         int value = arr[toppoint[stacknum]].data;         int lastindex = toppoint[stacknum];         toppoint[stacknum] = arr[toppoint[stacknum]].previous;         arr[lastindex] = null;         indexused--;          return value;     } }  public int peek(int stacknum) {     return arr[toppoint[stacknum]].data; }  public int nextavaindex() {     int index = -1;     if (indexused == arr.length || arr[indexused].data != 0) {         (int = 0; < arr.length; i++) {             if (arr[i].data == 0) {   // error                 index = i;                 break;             }         }         return index;      } else {         return indexused;     } }  public void print(int stacknum) {     while (toppoint[stacknum] != -1) {         system.out.print(arr[toppoint[stacknum]].data + "<--");         toppoint[stacknum] = arr[toppoint[stacknum]].previous;     } } public void printarr(){     for(int = 0; i< arr.length;i++){         system.out.print(arr[i]);     } } public class stacknode {  // exception in thread "main" java.lang.nullpointerexception 

list item

private int previous; private int data; public stacknode(int stacksize) { this.previous = -1; } public stacknode(int value, int prev) { data = value; previous = prev; } }

}

exception in thread "main" java.lang.nullpointerexception @ stackandqueue.flexiblemultistack$stacknode.access$000(flexiblemultistack.java:86) @ stackandqueue.flexiblemultistack.nextavaindex(flexiblemultistack.java:61) @ stackandqueue.flexiblemultistack.push(flexiblemultistack.java:32) @ stackandqueue.stackandqueue.main(stackandqueue.java:71) /users/xchen011/library/caches/netbeans/8.1/executor-snippets/run.xml:53: java returned: 1 build failed (total time: 0 seconds)

in pop() method, appears denoting open index setting array index null (arr[lastindex] = null). in nextavaindex() check if index available examining arr[i].data. if arr[i] has been set null pop(), nullpointerexception. make definition of available consistent check availability, try replacing arr[indexused].data != 0 arr[indexused] != null , if(arr[i].data == 0) if(arr[i] == null) in nextavaindex() method.

public int nextavaindex() {     int index = -1;     if (indexused == arr.length || arr[indexused] != null) {         (int = 0; < arr.length; i++) {             if (arr[i] == null) {   // error                 index = i;                 break;             }         }         return index;      } else {         return indexused;     } 

}


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -