Exception in thread "main" java.util.EmptyStackException
at java.util.Stack.peek(Stack.java:102)
at java.util.Stack.pop(Stack.java:84)
at com.company.PostFixExpression.solveExpression(PostFixExpression.java:49)
at com.company.PostFixExpression.main(PostFixExpression.java:14)
If you will try to pop an element out from the Stack Collection in Java when it is empty, you will get EmptyStackException exception. Vector peek() throws this exception if the stack is empty.
Fix:EmptyStackException is a Runtime Exception and as for all Runtime Exceptions, it is the duty of the developer to handle it. So before you pop an element out from the Stack, check if the Stack object is empty or not.
Example: Throw a custom exceptionStack stack = new Stack();
if(stack.isEmpty()) {
throw new Exception("Stack is empty.. nothing to pop");
}
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- SharePoint installation - Appfabric installation failed because installer MSI returned with error code:1603 - SharePoint
- How to start/boot macOS in safe mode - Big Sur 11.0, Catalina 10.15, or Mojave 10.14 - MacOS
- [Android Studio] Hardcoded string Button, should use @string resource - Android-Studio
- reCaptcha Verification expired. Check the checkbox again - Html
- [fix] Docker Desktop App not starting on Mac (macOS) - Docker
- How to place two div elements next to each other - CSS
- How to check RAM details on Mac? - MacOS
- Sign in as different user missing in SharePoint 2013 how to enable - SharePoint