我正在學習 Spring Boot。我試圖通過呼叫它的方法從服務類的 ArrayList 中獲取一個值。但是我得到了一個 NullPointerException。我添加了@Autowired 和@Service 注釋,它不起作用。我還查找了一些有關 Loc 容器的主題,但似乎沒有幫助。
這是我的主控制器:
package com.example.ecommerce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.ArrayList;
@Controller
public class MainController {
@Autowired
private ProductService productService;
//I'm getting a NullPointerException here when I call the method "getAllProduct"
ArrayList<Product> products = productService.getAllProduct();
@GetMapping("/")
public String home(){
return "home";
}
@GetMapping("/products")
public String product(Model model){
model.addAttribute("products",products);
return "product";
}
}
這是我的服務類:
package com.example.ecommerce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Service
public class ProductService {
ArrayList<Product> products = new ArrayList<>(Arrays.asList(
new Product("iPhone 4","Apple",1000),
new Product("iPhone 5","Banana",2000),
new Product("iPhone 6","Orange",3000)
));
public ArrayList<Product> getAllProduct(){
return products;
}
}
這是模型類產品:
package com.example.ecommerce;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
private String name;
private String brand;
private double price;
public Product() {
}
public Product(String name, String brand, double price) {
this.name = name;
this.brand = brand;
this.price = price;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product{"
"id=" id
", name='" name '\''
", brand='" brand '\''
", price=" price
'}';
}
}
編輯:能夠通過在 MainController 中添加建構式來修復它
private final ProductService productService;
private final ArrayList<Product> products;
@Autowired
public MainController(ProductService productService) {
this.productService = productService;
this.products = productService.getAllProduct();
}
uj5u.com熱心網友回復:
在注入依賴之前呼叫服務方法。這就是您獲得 NPE 的原因。嘗試使用建構式依賴注入
@Controller
public class MainController {
public MainController(ProductService productService) {
this.productService = productService;
}
private ProductService productService;
//I'm getting a NullPointerException here when I call the method "getAllProduct"
ArrayList<Product> products = productService.getAllProduct();
@GetMapping("/")
public String home() {
return "home";
}
@GetMapping("/products")
public String product(Model model) {
model.addAttribute("products", products);
return "product";
}
}
或者在控制器方法里面呼叫service方法
@Controller
public class MainController {
@Autowired
private ProductService productService;
@GetMapping("/")
public String home() {
return "home";
}
@GetMapping("/products")
public String product(Model model) {
model.addAttribute("products", productService.getAllProduct());
return "product";
}
}
uj5u.com熱心網友回復:
您的產品永遠不會在產品服務類中初始化,我建議您在 ProductService 類中嘗試這個
@Service
public class ProductService {
ArrayList<Product> products;
ProductService()
{
this.products= new ArrayList<>(Arrays.asList(
new Product("iPhone 4","Apple",1000),
new Product("iPhone 5","Banana",2000),
new Product("iPhone 6","Orange",3000)
));
}
public ArrayList<Product> getAllProduct(){
return products;
}
}
uj5u.com熱心網友回復:
很好,總是使用建構式注入而不是欄位注入。我只是在傳達 Spring 的建議。這是最近采用最多的轉換:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/352333.html
