java - What is a stack trace, and how can I use it to debug my application errors? -
sometimes when run application gives me error looks like:
exception in thread "main" java.lang.nullpointerexception @ com.example.myproject.book.gettitle(book.java:16) @ com.example.myproject.author.getbooktitles(author.java:25) @ com.example.myproject.bootstrap.main(bootstrap.java:14)
people have referred "stack trace". what stack trace? can tell me error that's happening in program?
about question - quite see question come through novice programmer "getting error", , paste stack trace , random block of code without understanding stack trace or how can use it. question intended reference novice programmers might need understanding value of stack trace.
in simple terms, stack trace list of method calls application in middle of when exception thrown.
simple example
with example given in question, can determine exception thrown in application. let's have @ stack trace:
exception in thread "main" java.lang.nullpointerexception @ com.example.myproject.book.gettitle(book.java:16) @ com.example.myproject.author.getbooktitles(author.java:25) @ com.example.myproject.bootstrap.main(bootstrap.java:14)
this simple stack trace. if start @ beginning of list of "at ...", can tell our error happened. we're looking topmost method call part of our application. in case, it's:
at com.example.myproject.book.gettitle(book.java:16)
to debug this, can open book.java
, @ line 16
, is:
15 public string gettitle() { 16 system.out.println(title.tostring()); 17 return title; 18 }
this indicate (probably title
) null
in above code.
example chain of exceptions
sometimes applications catch exception , re-throw cause of exception. typically looks like:
34 public void getbookids(int id) { 35 try { 36 book.getid(id); // method throws nullpointerexception on line 22 37 } catch (nullpointerexception e) { 38 throw new illegalstateexception("a book has null property", e) 39 } 40 }
this might give stack trace looks like:
exception in thread "main" java.lang.illegalstateexception: book has null property @ com.example.myproject.author.getbookids(author.java:38) @ com.example.myproject.bootstrap.main(bootstrap.java:14) caused by: java.lang.nullpointerexception @ com.example.myproject.book.getid(book.java:22) @ com.example.myproject.author.getbookids(author.java:36) ... 1 more
what's different 1 "caused by". exceptions have multiple "caused by" sections. these, typically want find "root cause", 1 of lowest "caused by" sections in stack trace. in our case, it's:
caused by: java.lang.nullpointerexception <-- root cause @ com.example.myproject.book.getid(book.java:22) <-- important line
again, exception we'd want @ line 22
of book.java
see might cause nullpointerexception
here.
more daunting example library code
usually stack traces more complex 2 examples above. here's example (it's long one, demonstrates several levels of chained exceptions):
javax.servlet.servletexception: bad happened @ com.example.myproject.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:60) @ org.mortbay.jetty.servlet.servlethandler$cachedchain.dofilter(servlethandler.java:1157) @ com.example.myproject.exceptionhandlerfilter.dofilter(exceptionhandlerfilter.java:28) @ org.mortbay.jetty.servlet.servlethandler$cachedchain.dofilter(servlethandler.java:1157) @ com.example.myproject.outputbufferfilter.dofilter(outputbufferfilter.java:33) @ org.mortbay.jetty.servlet.servlethandler$cachedchain.dofilter(servlethandler.java:1157) @ org.mortbay.jetty.servlet.servlethandler.handle(servlethandler.java:388) @ org.mortbay.jetty.security.securityhandler.handle(securityhandler.java:216) @ org.mortbay.jetty.servlet.sessionhandler.handle(sessionhandler.java:182) @ org.mortbay.jetty.handler.contexthandler.handle(contexthandler.java:765) @ org.mortbay.jetty.webapp.webappcontext.handle(webappcontext.java:418) @ org.mortbay.jetty.handler.handlerwrapper.handle(handlerwrapper.java:152) @ org.mortbay.jetty.server.handle(server.java:326) @ org.mortbay.jetty.httpconnection.handlerequest(httpconnection.java:542) @ org.mortbay.jetty.httpconnection$requesthandler.content(httpconnection.java:943) @ org.mortbay.jetty.httpparser.parsenext(httpparser.java:756) @ org.mortbay.jetty.httpparser.parseavailable(httpparser.java:218) @ org.mortbay.jetty.httpconnection.handle(httpconnection.java:404) @ org.mortbay.jetty.bio.socketconnector$connection.run(socketconnector.java:228) @ org.mortbay.thread.queuedthreadpool$poolthread.run(queuedthreadpool.java:582) caused by: com.example.myproject.myprojectservletexception @ com.example.myproject.myservlet.dopost(myservlet.java:169) @ javax.servlet.http.httpservlet.service(httpservlet.java:727) @ javax.servlet.http.httpservlet.service(httpservlet.java:820) @ org.mortbay.jetty.servlet.servletholder.handle(servletholder.java:511) @ org.mortbay.jetty.servlet.servlethandler$cachedchain.dofilter(servlethandler.java:1166) @ com.example.myproject.opensessioninviewfilter.dofilter(opensessioninviewfilter.java:30) ... 27 more caused by: org.hibernate.exception.constraintviolationexception: not insert: [com.example.myproject.myentity] @ org.hibernate.exception.sqlstateconverter.convert(sqlstateconverter.java:96) @ org.hibernate.exception.jdbcexceptionhelper.convert(jdbcexceptionhelper.java:66) @ org.hibernate.id.insert.abstractselectingdelegate.performinsert(abstractselectingdelegate.java:64) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:2329) @ org.hibernate.persister.entity.abstractentitypersister.insert(abstractentitypersister.java:2822) @ org.hibernate.action.entityidentityinsertaction.execute(entityidentityinsertaction.java:71) @ org.hibernate.engine.actionqueue.execute(actionqueue.java:268) @ org.hibernate.event.def.abstractsaveeventlistener.performsaveorreplicate(abstractsaveeventlistener.java:321) @ org.hibernate.event.def.abstractsaveeventlistener.performsave(abstractsaveeventlistener.java:204) @ org.hibernate.event.def.abstractsaveeventlistener.savewithgeneratedid(abstractsaveeventlistener.java:130) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.savewithgeneratedorrequestedid(defaultsaveorupdateeventlistener.java:210) @ org.hibernate.event.def.defaultsaveeventlistener.savewithgeneratedorrequestedid(defaultsaveeventlistener.java:56) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.entityistransient(defaultsaveorupdateeventlistener.java:195) @ org.hibernate.event.def.defaultsaveeventlistener.performsaveorupdate(defaultsaveeventlistener.java:50) @ org.hibernate.event.def.defaultsaveorupdateeventlistener.onsaveorupdate(defaultsaveorupdateeventlistener.java:93) @ org.hibernate.impl.sessionimpl.firesave(sessionimpl.java:705) @ org.hibernate.impl.sessionimpl.save(sessionimpl.java:693) @ org.hibernate.impl.sessionimpl.save(sessionimpl.java:689) @ sun.reflect.generatedmethodaccessor5.invoke(unknown source) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:25) @ java.lang.reflect.method.invoke(method.java:597) @ org.hibernate.context.threadlocalsessioncontext$transactionprotectionwrapper.invoke(threadlocalsessioncontext.java:344) @ $proxy19.save(unknown source) @ com.example.myproject.myentityservice.save(myentityservice.java:59) <-- relevant call (see notes below) @ com.example.myproject.myservlet.dopost(myservlet.java:164) ... 32 more caused by: java.sql.sqlexception: violation of unique constraint my_entity_uk_1: duplicate value(s) column(s) my_column in statement [...] @ org.hsqldb.jdbc.util.throwerror(unknown source) @ org.hsqldb.jdbc.jdbcpreparedstatement.executeupdate(unknown source) @ com.mchange.v2.c3p0.impl.newproxypreparedstatement.executeupdate(newproxypreparedstatement.java:105) @ org.hibernate.id.insert.abstractselectingdelegate.performinsert(abstractselectingdelegate.java:57) ... 54 more
in example, there's lot more. we're concerned looking methods our code, in com.example.myproject
package. second example (above), we'd first want down root cause, is:
caused by: java.sql.sqlexception
however, method calls under library code. we'll move "caused by" above it, , first method call originating our code, is:
at com.example.myproject.myentityservice.save(myentityservice.java:59)
like in previous examples, should @ myentityservice.java
on line 59
, because that's error originated (this one's bit obvious went wrong, since sqlexception states error, debugging procedure we're after).
Comments
Post a Comment