I am getting a run error but my output is coming fine.what is wrong in my code?

import java.util.*;
public class Main {
static Scanner s = new Scanner(System.in);
public static void main(String args[]) throws Exception {
// Your Code Here

    int q = s.nextInt();
    Main obj = new Main();
    StacksUsingArrays stack = obj.new StacksUsingArrays();
	Calculate(stack, q);
}

public static void Calculate(StacksUsingArrays stack, int q) throws Exception {

   //Write Your Code Here
   /* Donot initialize another Scanner use the static scanner already declared*/
   	for (int i = 0; i < q; i++) {
		
		int n = s.nextInt();
		if (n == 2) {
			int cost = s.nextInt();
			stack.push(cost);
		}
		else {
			int item=stack.pop();
			System.out.println(item);
		}
		
		
		
	}
	
}

private class StacksUsingArrays {
	private int[] data;
	private int tos;

	public static final int DEFAULT_CAPACITY = 10;

	public StacksUsingArrays() throws Exception {
		// TODO Auto-generated constructor stub
		this(DEFAULT_CAPACITY);
	}

	public StacksUsingArrays(int capacity) throws Exception {
		if (capacity <= 0) {
			System.out.println("Invalid Capacity");
		}
		this.data = new int[capacity];
		this.tos = -1;
	}

	public int size() {
		return this.tos + 1;
	}

	public boolean isEmpty() {
		if (this.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public void push(int item) throws Exception {
		if (this.size() == this.data.length) {
		    
		    int[] temp = new int[2 * data.length];
		    for(int i = 0;i < data.length;i++){
		        temp[i] = data[i];
		    }
		    
		    data = temp;
		}
		this.tos++;
		this.data[this.tos] = item;
	}

	public int pop() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		int retVal = this.data[this.tos];
		this.data[this.tos] = 0;
		this.tos--;
		return retVal;
	}

	public int top() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		int retVal = this.data[this.tos];
		return retVal;
	}

	public void display() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		for (int i = this.tos; i >= 0; i--) {
			System.out.println(this.data[i]);
		}

	}

}

}

if you pop from an empty stack, that may result in a run error. Also, you have not considered this as well

If the pile is empty, print “No Code” (without the quotes)

can u explain what u want to say ?

can u share the corrected code???

You have to print “NO CODE” when stack is empty and student wants to buy course.
go through this code

please mark your doubt as resolved and rate as well :slight_smile: