从服务调用方法时出现 SpringBoot @Autowired NullPointerException class

SpringBoot @Autowired NullPointerException when calling method from service class

我正在学习Spring引导。我正在尝试通过调用其方法从服务 class 的 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";
    }
}

这是我的服务class:

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

这是型号class产品:

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

在注入依赖项之前调用服务方法。这就是您获得 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";
}
}

或者在controller方法里面调用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";
}
}

您的产品从未在产品服务 class 中初始化,我建议您在 ProductService class

中尝试此操作
    @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;
                         } 
                    }

很好,总是使用构造函数注入而不是字段注入。我只是转发 Spring 的建议。这是最近采用最多的转换:)