{"id":643,"date":"2016-08-08T19:31:52","date_gmt":"2016-08-08T18:31:52","guid":{"rendered":"http:\/\/www.voldrich.net\/?p=643"},"modified":"2020-03-27T17:00:21","modified_gmt":"2020-03-27T15:00:21","slug":"nullpointerexception-thrown-from-out-of-nowhere","status":"publish","type":"post","link":"https:\/\/www.voldrich.net\/index.php\/2016\/08\/08\/nullpointerexception-thrown-from-out-of-nowhere\/","title":{"rendered":"NullPointerException thrown from out of nowhere"},"content":{"rendered":"<p>Today I spent 30 minutes debugging a code which was throwing a null pointer exception. I never seen it before so I ruled out the obvious suspects and checked during several debugging runs what is and what actually is not null. It was not my code and it looked a bit shady, but everything seemed fine in debugger and more importantly, nothing seemed to be null.<\/p>\n<p><!--more--><\/p>\n<p>Finally after banging my head a bit I traced it down to damned unboxing null pointer.<\/p>\n<p>Consider following code:<\/p>\n<p>[sourcecode language=&#8221;java&#8221;]<br \/>\nclass Test {<br \/>\n\tpublic Integer getRowType() {<br \/>\n\t\treturn null;<br \/>\n\t}<br \/>\n\tpublic void handleRowType(int rowType) {<br \/>\n\t\t\/\/ not relevant<br \/>\n\t}<\/p>\n<p>\tpublic void main(String[] args) {<br \/>\n\t\thandleRowType(getRowType()); \/\/ NPE is thrown on this line.<br \/>\n\t}<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>If you look at it like this then the error is \u00a0obvious to the experienced eye. getRowType returns null Integer. handleRowType method requires primitive int type so Java auto-unboxes the supplied Integer and throws the NPE because the Integer returned is null. Hopefully this will save someone few bangs to the head :).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today I spent 30 minutes debugging a code which was throwing a null pointer exception. I never seen it before so I ruled out the obvious suspects and checked during several debugging runs what is and what actually is not null. It was not my code and it looked a bit shady, but everything seemed [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"templates\/template-full-width.php","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/comments?post=643"}],"version-history":[{"count":1,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":702,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/643\/revisions\/702"}],"wp:attachment":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/media?parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/categories?post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/tags?post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}