I'm using a try-catch block to catch any exceptions that might be thrown when I run certain methods on a stack. My code for the pop method looks like this:
public T pop() { T temp = null; try { temp = first.data; } catch(Exception e) { System.out.println("Invalid operation on an empty stack"); } first = first.next; return temp; } When I run the program the exception is caught... my terminal produces this: (menu choice 'd' is the pop method)
Please enter a menu choice: d
Invalid operation on an empty stack Exception in thread "main" java.lang.NullPointerException at MyStack.pop(MyStack.java:58) at StackTest.main(StackTest.java:52)
so I think my exception is being caught since it prints "Invalid operation...", but my program also terminates when the exception is thrown. Any help?
firstis null