Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read and understand the java stack trace? [duplicate]

For example, I got a stack trace like this:

java.lang.NullPointerException abc.investxa.presentation.controllers.UnixServerJobController.handleRequest(UnixServerJobController.java:66) org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501) javax.servlet.http.HttpServlet.service(HttpServlet.java:690) javax.servlet.http.HttpServlet.service(HttpServlet.java:803) org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96) org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 

So what is the root cause of this exception? From the stack trace, I found out that there is a problem with doFilter function in the OncePerRequestFilter class! However, when I put a break point there and the program never stop at that break point.

Could anyone give explanation about this!? And in general case, how should I use that stack case for debugging (read from bottom to top or from top to bottom)!

like image 482
Toby D Avatar asked Oct 02 '12 09:10

Toby D


People also ask

How do you read a stack trace?

To read this stack trace, start at the top with the Exception's type - ArithmeticException and message The denominator must not be zero . This gives an idea of what went wrong, but to discover what code caused the Exception, skip down the stack trace looking for something in the package com.

How does stack trace work in Java?

Simply put, a stack trace is a representation of a call stack at a certain point in time, with each element representing a method invocation. The stack trace contains all invocations from the start of a thread until the point it's generated. This is usually a position at which an exception takes place.

Do you read a stack trace from top to bottom?

The first line in the call stack represents the last executed function call, so remember to always read a stack trace top-down.

What is backtrace Java?

What's a Java Stack Trace? A stack trace, also called a stack backtrace or even just a backtrace, is a list of stack frames. These frames represent a moment during an application's execution. A stack frame is information about a method or function that your code called.


1 Answers

You should generally read from the top - so in this case, there's a NullPointerException at line 66 of UnixServerJobController, in the handleRequest method. That method was called by SimpleControllerHandlerAdapter.handle, which was called by DispatcherServlet.doDispatch etc.

However, in this particular case it's likely that the first frame of the stack trace is all you need. Look at line 66 of UnixServerJobController, work out what might be null, and act accordingly.

Note that sometimes one exception is wrapped in another (which may in turn be wrapped in another, etc). In this case you should look at each of the stack traces - often it's the "most nested" exception which gives the most useful information, as that's the root cause.

like image 56
Jon Skeet Avatar answered Sep 25 '22 10:09

Jon Skeet