Playframework:[RuntimeException:java.lang.reflect.InvocationTargetException]

编程入门 行业动态 更新时间:2024-10-24 09:17:23
本文介绍了Playframework:[RuntimeException:java.lang.reflect.InvocationTargetException]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试基于Zentask示例创建一个简单的登录 - zentask - playframework ,但是当我单击调用Application.authenticate操作的登录按钮时,它会给出运行时异常。我已经标记了行 - 错误

I'm trying to create a simple login based on the Zentask sample --zentask - playframework, however when I click the login button which calls the Application.authenticate actions, it gives runtime exception. I have marked the line with -- error

[RuntimeException: java.lang.reflect.InvocationTargetException]

Application.java

Application.java

public class Application extends Controller { ......... public static class Login { public String email; public String password; public String validate() { if (User.authenticate(email, password) == null) { return "Invalid user or password"; } return null; } } public static Result authenticate() { Form<Login> loginForm = form(Login.class).bindFromRequest(); //--- error if(loginForm.hasErrors()) { return badRequest(login.render(loginForm)); } else { session("email", loginForm.get().email); return redirect( routes.Application.index() ); } } }

我知道它有一些东西给使用Login Class中的validate函数,因为当我在validate函数中删除对User.authenticate的调用时,它可以正常工作。但是我无法弄明白。

I understand it has something to do with the validate function in Login Class, because when I remove the call to User.authenticate in the validate function it works without error. But I am unable to figure it out.

用户类是 -

@Entity public class User extends Model { @Id @Constraints.Required @Formats.NonEmpty public String userId; @OneToOne(cascade=CascadeType.PERSIST) AccountDetails accDetails; public static Model.Finder<String,User> find = new Model.Finder<String,User>(String.class, User.class); // Authenticate the user details public static User authenticate(String email, String password) { String tempId = AccountDetails.authenticate(email, password).userId; return find.ref(tempId); } .. . . . . . . }

和AccountDetails类 -

and the AccountDetails Class -

@Entity public class AccountDetails extends Model { @Id String userId; @Constraints.Required String emailId; @Constraints.Required String password; public static Model.Finder<String,AccountDetails> find = new Model.Finder<String,AccountDetails>(String.class, AccountDetails.class); public static AccountDetails authenticate(String email, String password) { return find.where() .eq("email", email) .eq("password", password) .findUnique(); } }

推荐答案

我必须承担很多,但如果这是你的堆栈跟踪的样子:

I have to assume a lot but if this is what your stacktrace looked like:

play.api.Application$$anon$1: Execution exception[[RuntimeException: java.lang.reflect.InvocationTargetException]] at play.api.Application$class.handleError(Application.scala:293) ~[play_2.10.jar:2.2.3] at play.api.DefaultApplication.handleError(Application.scala:399) [play_2.10.jar:2.2.3] at play.core.serverty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.3] at play.core.serverty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.3] at scala.Option.map(Option.scala:145) [scala-library.jar:na] at play.core.serverty.PlayDefaultUpstreamHandler$$anonfun$3.applyOrElse(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.3] ... Caused by: java.lang.NullPointerException: null at models.User.authenticate(User.java:26) ~[na:na] at controllers.Application$Login.validate(Application.java:50) ~[na:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_05] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_05] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_05] at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0_05]

然后问题的原因实际上是由您的User类中的NPE暗示的。 如果您输入伪造凭证,您的查找程序将找不到任何内容并在AccountDetails.authenticate()中返回null。

then the cause of the problem is actually hinted at by the NPE in your User class. If you enter bogus credentials, your finder will not find anything and return null in AccountDetails.authenticate().

所以在下面的方法中,您不检查for null并尝试获取userId,这会导致NPE:

So in the method below you do not check for null and try and get the userId, which causes the NPE:

public static User authenticate(String email, String password) { String tempId = AccountDetails.authenticate(email, password).userId; return find.ref(tempId); }

如果您只是正确检查null,您将获得所需的功能:

If you just check for null properly you will get the desired functionality:

public static User authenticate(String email, String password) { User user = null; AccountDetails accountDetails = AccountDetails.authenticate(email, password); if (accountDetails != null) { user = find.ref(accountDetails.userId); } return user; }

更多推荐

Playframework:[RuntimeException:java.lang.reflect.InvocationTargetException]

本文发布于:2023-10-28 23:34:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1538091.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:java   RuntimeException   Playframework   InvocationTargetException   reflect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!