Null pointer Exception while reversing a Queue using stack

I was practicing on how to implement queue using arrays. I have easily implemented how to enqueue and dequeue the elements in queue. But I have encountered an exception while implementing reverse of queue using stacks

public class QueueImpl { private int capacity; int queueArr[]; int front = 0; int rear = -1; int currentSize = 0; QueueImpl(int queueSize){     this.capacity=queueSize;     queueArr=new int[this.capacity]; }  public void enqueue(int data){     if(isQueueFull()){         System.out.println("Overflow");         return;     }     else{         rear=rear+1;         if(rear==capacity-1)         {             rear=0;         }         queueArr[rear]=data;         currentSize++;         System.out.println("Element " + data+ " is pushed to Queue !");     }  }   public int dequeue(){     if(isQueueEmpty()){         System.out.println("UnderFlow");     }     else{         front=front+1;         if(front == capacity-1){             System.out.println("Pop operation done ! removed: "+queueArr[front-1]);             front = 0;         } else {             System.out.println("Pop operation done ! removed: "+queueArr[front-1]);         }         currentSize--;     }     return queueArr[front-1];  } private boolean isQueueEmpty() {     boolean status=false;     if(currentSize==0){         status=true;     }     return status; }  private boolean isQueueFull() {     boolean status=false;     if(currentSize==capacity){         status=true;     }     return status; }  public static void main(String arg[]) {     QueueImpl queueImpl=new QueueImpl(5);     queueImpl.enqueue(5);     queueImpl.enqueue(2);     queueImpl.enqueue(9);     queueImpl.enqueue(1); //  queueImpl.dequeue();     queueImpl.printQueue(queueImpl);     queueImpl.reverse(queueImpl); }  private void printQueue(QueueImpl queueImpl) { System.out.println(queueImpl.toString());        }  @Override     public String toString() {         return "Queue [front=" + front + ", rear=" + rear + ", size=" + currentSize                 + ", queue=" + Arrays.toString(queueArr) + "]";     } private QueueImpl reverse(QueueImpl queueImpl) throws ArrayIndexOutOfBoundsException {     int i=0;     Stack<Integer> stack=new Stack<Integer>();       while(!queueImpl.isQueueEmpty()){         stack.push(queueImpl.dequeue());     }     while(!stack.isEmpty()){         stack.get(i);         i++;     }     while(!stack.isEmpty()){         queueImpl.enqueue(stack.pop());     }     return queueImpl; }  } 

The error log is -

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1  at com.tcs.QueueUsingAraay.QueueImpl.dequeue(QueueImpl.java:51)  at com.tcs.QueueUsingAraay.QueueImpl.reverse(QueueImpl.java:93)  at com.tcs.QueueUsingAraay.QueueImpl.main(QueueImpl.java:78) 

Replay

Category: data structures Time: 2016-07-31 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.220 (s). 12 q(s)