gpt4 book ai didi

java - 在 Controller 中注入(inject)构造函数时出错,java.lang.NullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:47:35 24 4
gpt4 key购买 nike

我收到以下错误消息:

ProvisionException: Unable to provision, see the following errors:

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 error

这是堆栈跟踪:

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

我不太确定我该怎么做。我一直在到处搜索,找不到任何有用的东西。想法?

最佳答案

我想通了,我需要将我的代码更改为如下所示:

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 的构造函数来注入(inject):

private Form<Product> productForm;

@Inject
public ProductsController(FormFactory formFactory) {
this.productForm = formFactory.form(Product.class);
}

关于java - 在 Controller 中注入(inject)构造函数时出错,java.lang.NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49974277/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com