Getting java.lang.NullPointerException when calling Method.invoke

前端 未结 4 1218
耶瑟儿~
耶瑟儿~ 2021-02-19 11:17

I\'m following this tutorial on Java annotaitons and implemented the Test annotation as shown there. But when running the code I get the following output.

java.l         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-19 12:01

    This issue is here:

    method.invoke(null);
    

    This method's first parameter is the object to invoke the method on. This is the dynamic (reflection) equivalent of something like this:

    Object foo = null;
    foo.toString();
    

    Of course we would expect this code to give a NullPointerException because foo is null.

提交回复
热议问题