问题描述
我收到以下错误消息:
1)错误注入构造函数,java.lang.NullPointerException在
controllers.ProductsController。(ProductsController.java:20)
同时定位controllers.ProductsController
为router.Routes。(Routes.scala:34)的第4个参数,同时定位router.Routes同时定位
play.api.inject.RoutesProvider同时定位
play.api.routing.Router
为play.api.http.JavaCompatibleHttpRequestHandler的第一个参数。(HttpRequestHandler.scala:222)
同时定位play.api.http.JavaCompatibleHttpRequestHandler而
定位play.api.http.HttpRequestHandler
为play.api.DefaultApplication的第6个参数。(Application.scala:236)
play.api.DefaultApplication.class(Application.scala:235),而
定位play.api .DEF找到
时的aultApplication.api.Application
1) Error injecting constructor, java.lang.NullPointerException at controllers.ProductsController.(ProductsController.java:20)
while locating controllers.ProductsController for the 4th parameter of router.Routes.(Routes.scala:34) while locating router.Routes while locating play.api.inject.RoutesProvider while locating play.api.routing.Router for the 1st parameter of play.api.http.JavaCompatibleHttpRequestHandler.(HttpRequestHandler.scala:222) while locating play.api.http.JavaCompatibleHttpRequestHandler while locating play.api.http.HttpRequestHandler for the 6th parameter of play.api.DefaultApplication.(Application.scala:236) at play.api.DefaultApplication.class(Application.scala:235) while locating play.api.DefaultApplication while locating play.api.Application
1错误
这是堆栈跟踪:
com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at controllers.ProductsController.<init>(ProductsController.java:20)
while locating controllers.ProductsController
for the 4th parameter of router.Routes.<init>(Routes.scala:34)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
for the 1st parameter of play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:222)
while locating play.api.http.JavaCompatibleHttpRequestHandler
while locating play.api.http.HttpRequestHandler
for the 6th parameter of play.api.DefaultApplication.<init>(Application.scala:236)
at play.api.DefaultApplication.class(Application.scala:235)
while locating play.api.DefaultApplication
while locating play.api.Application
1 error
com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1028)
com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1054)
play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:409)
play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:404)
这是我的代码:
package controllers;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import play.mvc.Controller;
import play.mvc.Result;
import play.data.Form;
import play.data.FormFactory;
import views.html.products.details;
import views.html.products.list;
import models.Product;
public class ProductsController extends Controller {
@Inject FormFactory formFactory;
private Form<Product> productForm = formFactory.form(Product.class);
public Result list() {
List<Product> products = Product.findAll();
return ok(list.render(products));
}
public Result newProduct() {
return ok(details.render(productForm));
}
public Result details(String ean) {
return TODO;
}
public Result save() {
return TODO;
}
}
我真的不知道该怎么做。我一直在寻找各地,找不到任何有用的东西。想法?
I'm not really sure what should I do. I've been searching all over the place and couldn't find anything useful. Thoughts?
推荐答案
我想出来了,我需要将我的代码更改为如下所示:
I figured this out, I needed to change my code to be like the following:
package controllers;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import play.mvc.Controller;
import play.mvc.Result;
import play.data.Form;
import play.data.FormFactory;
import views.html.products.details;
import views.html.products.list;
import models.Product;
public class ProductsController extends Controller {
private Form<Product> productForm;
@Inject
public ProductsController(FormFactory formFactory) {
this.productForm = formFactory.form(Product.class);
}
public Result list() {
List<Product> products = Product.findAll();
return ok(list.render(products));
}
public Result newProduct() {
return ok(details.render(productForm));
}
public Result details(String ean) {
return TODO;
}
public Result save() {
return TODO;
}
}
具体来说,我需要一个构造函数要注入的 FormFactory
:
To be specific, I needed to have a constructor with a FormFactory
to be injected:
private Form<Product> productForm;
@Inject
public ProductsController(FormFactory formFactory) {
this.productForm = formFactory.form(Product.class);
}
这篇关于在控制器上注入构造函数java.lang.NullPointerException时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!