We see a NullPointerException, which is a common mistake for Java programmers. The first line tells us the exact error that caused the program to print a stack trace. Exception in thread "main" Īt .getTitle(Book.java:16)Īt .getBookTitles(Author.java:25)Īt .main(Bootstrap.java:14) Let’s take a look at the Java stack trace below. Stack traces are constructed in a very similar way in most languages: they follow the LIFO stack approach. Now that you have a deeper understanding of how a stack trace works, let’s learn to read one. Therefore, reading the first line of the stack trace shows you the exact function call that threw an error. The stack trace first prints the function call that caused the error and then prints the previous underlying calls that led up to the faulty call. This also implies that a stack trace is printed top-down. This is due to the LIFO behavior of the collection that keeps track of underlying, previous function calls. Whenever a certain function call throws an error, you’ll have a collection of function calls that lead up to the call that caused the particular problem. Each element in this collection represents a function call in your code that contains logic. The collection works as a last-in, first-out (LIFO) collection. To better understand what a call stack is, let’s dive a bit deeper into how programming languages work.Ī stack is actually a data type that contains a collection of elements. To put it simply, a stack trace represents a call stack at a certain point in time.
0 Comments
Leave a Reply. |