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

I am trying to create a simple login based on the example of Zentask - zentask - playframework , however, when I click on the login button that invokes the Application.authenticate action, it gives a runtime exception. I marked a line with an error

[RuntimeException: java.lang.reflect.InvocationTargetException] 

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() ); } } } 

I understand that this has something to do with the validate function in the input class, because when I delete the call to the User.authenticate function in the validation function, it works without errors. But I can’t understand it.

User Class -

 @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); } .. . . . . . . } 

and class AccountDetails -

 @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(); } } 

Any help would be greatly appreciated.

Thanx a lot.

+8
java playframework
source share
1 answer

I have to accept the selection, but if this is what your stack 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.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$3.apply(PlayDefaultUpstreamHandler.scala:264) [play_2.10.jar:2.2.3] at play.core.server.netty.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.server.netty.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] 

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

So, in the method below, you are not checking for null and trying to get userId, which causes NPE:

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

If you check for zero correctly, 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; } 
+4
source share

All Articles