Sometimes when I run my application it gives me an error that looks like:
Exception in thread "main" java.lang.NullPointerException
at com.example.myproject.Book.getTitle(Book.java:16)
at com.example.myproject.Author.getBookTitles(Author.java:25)
at com.example.myproject.Bootstrap.main(Bootstrap.java:14)
People have referred to this as a “stack trace”. What is a stack trace? What can it tell me about the error that’s happening in my program?
About this question – quite often I see a question come through where a novice programmer is “getting an error”, and they simply paste their stack trace and some random block of code without understanding what the stack trace is or how they can use it. This question is intended as a reference for novice programmers who might need help understanding the value of a stack trace.
In simple terms, a stack trace is a list of the method calls that the application was in the middle of when an Exception was thrown.
Simple Example
With the example given in the question, we can determine exactly where the exception was thrown in the application. Let’s have a look at the stack trace:
This is a very simple stack trace. If we start at the beginning of the list of "at …", we can tell where our error happened. What we’re looking for is the topmost method call that is part of our application. In this case, it’s:
To debug this, we can open up
Book.javaand look at line16, which is:This would indicate that something (probably
title) isnullin the above code.Example with a chain of exceptions
Sometimes applications will catch an Exception and re-throw it as the cause of another Exception. This typically looks like:
This might give you a stack trace that looks like:
What’s different about this one is the "Caused by". Sometimes exceptions will have multiple "Caused by" sections. For these, you typically want to find the "root cause", which will be one of the lowest "Caused by" sections in the stack trace. In our case, it’s:
Again, with this exception we’d want to look at line
22ofBook.javato see what might cause theNullPointerExceptionhere.More daunting example with library code
Usually stack traces are much more complex than the two examples above. Here’s an example (it’s a long one, but demonstrates several levels of chained exceptions):
In this example, there’s a lot more. What we’re mostly concerned about is looking for methods that are from our code, which would be anything in the
com.example.myprojectpackage. From the second example (above), we’d first want to look down for the root cause, which is:However, all the method calls under that are library code. So we’ll move up to the "Caused by" above it, and in that "Caused by" block, look for the first method call originating from our code, which is:
Like in previous examples, we should look at
MyEntityService.javaon line59, because that’s where this error originated (this one’s a bit obvious what went wrong, since the SQLException states the error, but the debugging procedure is what we’re after).