在控制器上注入构造函数java.lang.NullPointerException时出错

时间:2018-04-23 05:57:39

标签: java playframework

我收到以下错误消息:

  

ProvisionException:无法配置,请参阅以下错误:

     

1)注入构造函数时出错,java.lang.NullPointerException at   controllers.ProductsController。(ProductsController.java:20)
  在定位controllers.ProductsController时       对于router.Routes。(Routes.scala:34)的第4个参数,同时定位router.Routes while locate   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)at   play.api.DefaultApplication.class(Application.scala:235)while   在定位时找到play.api.DefaultApplication   play.api.Application

     

1错误

这里是Stack Trace:

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

我不确定我该怎么做。我一直在寻找各地,无法找到有用的东西。想法?

1 个答案:

答案 0 :(得分:1)

我想出来了,我需要将我的代码更改为以下内容:

.htaccess

具体来说,我需要一个注入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