groovy code "(wangwang as Man).shout(10)" throws java.lang.NullPointerException -


i new groovy. wrote below code

public interface man{     public void say();     public int shout(int x); }  def wangwang = {     println("wangwang!");     println(it) }  //(wangwang man).say()  (wangwang man).shout(10) 

i run in groovyconsole. , here's output

wangwang! 10 exception thrown  java.lang.nullpointerexception     @ com.sun.proxy.$proxy18.shout(unknown source)     @ man$shout.call(unknown source)     @ consolescript10.run(consolescript10:13) 

it has prints out "10", why throw exception?

very interesting scenario, indeed! i'd that's because groovy tries cast null value returned println int defined interface. if change return type of method shout void problem goes away.

the problem goes away if approach other side - if make closure return value e.g.

def wangwang = {    println("wangwang!");    println(it)    } 

or maybe if try running code below may make matter clearer

(println (10)).class 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -