Issue
I'm learning Spring Boot. I'm trying to fetch a value from an ArrayList from the service class by calling its method. But I got a NullPointerException. I add both @Autowired and @Service annotations, it doesn't work. I also looked up some topics about Loc container but it doesn't seem to help.
Here is my MainController:
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";
}
}
Here is my service 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;
}
}
Here is the model class Product:
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 +
'}';
}
}
Edit: was able to fix it by adding a constructor in the MainController
private final ProductService productService;
private final ArrayList<Product> products;
@Autowired
public MainController(ProductService productService) {
this.productService = productService;
this.products = productService.getAllProduct();
}
Solution
The service method is called before the dependency is injected. That's why you are getting NPE. Try using constructor dependency injection
@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";
}
}
Or call the service method inside the controller method
@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";
}
}
Answered By - Rahil Husain