未处理的异常类型NoSuchFieldException(java反射)
我使用 Play Framework 大约 1 个月,这是一件很棒的事情,但我遇到了一个大问题 。 我尝试在安全控制器中运行以下代码:
MyModel myModel = MyModel.all().first();
Field idField = myModel.getClass().getField("id");
关于第 2 行 Play 说: 编译错误
The file /app/controllers/Security.java could not be compiled. Error
raised is : Unhandled exception type NoSuchFieldException
也许这是一个核心错误? 谢谢。
I am using Play Framework about 1 month and it is a great thing, but I had one big problem
.
I`ve try to run following code in secure controller:
MyModel myModel = MyModel.all().first();
Field idField = myModel.getClass().getField("id");
About line 2 Play says:
Compilation error
The file /app/controllers/Security.java could not be compiled. Error
raised is : Unhandled exception type NoSuchFieldException
Maybe it`s a core bug?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该处理 getField(String fieldName) 可能引发的异常。在本例中为 NoSuchFieldException。
尝试这样写:
You should handle the exception that getField(String fieldName) can throw. In this case a NoSuchFieldException.
Try to write it as such:
如果您使用 dp4j 的
@TestPrivates
或@Reflect(catchExceptions =true)< /code> 您不需要自己编写 catch 语句:
If you use dp4j's
@TestPrivates
or@Reflect(catchExceptions =true)
you don't need to write the catch statements yourself: