object is not an instance of declaring class 反射異常

在使用反射執行一個方法時常遇到object is not an instance of declaring class的異常,如下代碼:

 第34行會報object is not an instance of declaring class錯 對象不是聲明類的一個實例。解決辦法如下:

 第一種:反射執行的方法 getPrimaryKey() 改成靜態的

第二種:在執行方法前先實例化類。m.invoke(mothed,null)改爲m.invoke(c.newInstance(),null)或者m.invoke(new PrimaryKeyUtils(),null)

         

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章