问题
when i execute the below controller action I get the error attached at end of this question. when getting this error and if i refresh the page in browser the controller view page displays with no error. i'm not sure what causes this error at first request of beleow controller action?
/**
* controller to register new user.
* Shows registration screen.
*/
public static void registration() throws Exception {
ObjectType type = ObjectType.forClass("models.User");
Constructor<?> constructor = type.entityClass.getDeclaredConstructor();
constructor.setAccessible(true);
Model object = (Model) constructor.newInstance();
/*System.out.print("type=");
System.out.println(type);
System.out.print("object=");
System.out.println(object);*/
render(type, object);
}
----Exception error trace--------------
23:12:14,229 ERROR ~
@69bf92hlc
Internal Server Error (500) for request GET /registration
Template execution error (In {module:crud}/app/views/tags/crud/types.tag around line 3)
Execution error occured in template {module:crud}/app/views/tags/crud/types.tag. Exception raised was NullPointerException : null.
play.exceptions.TemplateExecutionException
at play.templates.BaseTemplate.throwException(BaseTemplate.java:86)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:257)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:379)
at {module:crud}/conf/routes.(line:4)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
at play.templates.Template.render(Template.java:26)
at play.templates.GroovyTemplate.render(GroovyTemplate.java:187)
at play.mvc.Router.parse(Router.java:162)
at play.mvc.Router.parse(Router.java:190)
at play.mvc.Router.parse(Router.java:164)
at play.mvc.Router.load(Router.java:48)
at play.mvc.Router.detectChanges(Router.java:219)
at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException
at play.classloading.ApplicationCompiler$2.acceptResult(ApplicationCompiler.java:266)
at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:478)
at play.classloading.ApplicationCompiler.compile(ApplicationCompiler.java:282)
at play.classloading.ApplicationClassloader.getAllClasses(ApplicationClassloader.java:424)
at play.classloading.ApplicationClassloader.getAssignableClasses(ApplicationClassloader.java:453)
at play.classloading.ApplicationClassloader$getAssignableClasses.call(Unknown Source)
at {module:crud}/app/views/tags/crud/types.tag.(line:3)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:232)
... 11 more
回答1:
I think the reason for you error is that when you first try display the form there's no object created yet, so the Exception raised was NullPointerException : null.
Unluckily I'm not familiar with Play 2.* and do not plan on starting before it's more stable, but I think I understood that CRUD generation is not included nor fully supported there, so you are probably using code from play1 crud?
I think the solution in your case is to better cover the New(blank) / View / Save pattern; seeing your Routes might also help to understand this precise issue.
In any case, considering you are not generating an unknown model type, rather always a User
one, what is the real advantage of going through this complicated pattern?
You should do something like
blank
User user = null
render()
view/edit
User user = User.findById(id);
save
public static void save(@Valid User object) {
if(validation.hasErrors()) {
params.flash(); // add http parameters to the flash scope
User user = object;
render("User/show.html", user);
}
}
来源:https://stackoverflow.com/questions/9220495/playframework-random-crud-error