JPA Repository.findByAll() returns null but everything exists in db

TomaszC283 :

I Have simple JSP page whichs should display 5 Products with id, description, amount. Can you help me to find a problem ?

I want to Connect this page with Database, manualy listing products will be not professional and problematic when you want to add more Products.

When i request page /storage i have an exception :

[ERROR] Servlet.service() for servlet [dispatcherServlet] in context with path [] 
threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
    at (...)StoragePageController.openAllProductsPage(StoragePageController.java:27)

Storage.JSP :

<table width="1000" border="0" cellpadding="6" cellspacing="2">
    <c:forEach var="u" items="${productList }">
        <c:set var="licznik" value="${licznik+1}" />
        <tr onmouseover="changeTrBg(this)" onmouseout="defaultTrBg(this)">
            <td align="right"><c:out value="${licznik }" /></td>
            <td align="right"><c:out value="${u.id }" /></td>
            <td align="left"><c:out value="${u.description }" /></td>
            <td align="left"><c:out value="${u.amount }" /></td>
        </tr>
    </c:forEach>
</table>

StoragePageController :

@Controller
public class StoragePageController {


    private StorageService storageService;

    @GET
    @RequestMapping(value = "/storage")
    public String openAllProductsPage(Model model) {

        List<Product> productList = storageService.findAll();
        model.addAttribute("productList", productList);
        return "storage";
    }
}

StorageService :

public interface StorageService {

    public Product findById(int id);
    List<Product> findAll();
    void updateProduct(int id, int amount);

}

StorageServiceImpl :

@Service("storageService")
@Transactional
public class StorageServiceImpl implements StorageService {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public Product findById(int id) {
        Product product = productRepository.findById(id);
        return product;
    }

    @Override
    public List<Product> findAll() {
        List<Product> productList = productRepository.findAll();
        return productList;
    }

    @Override
    public void updateProduct(int id, int amount) {
        productRepository.updateProductAmount(amount, id);
    }

}

ProductRepository :

@Repository("productRepository")
public interface ProductRepository extends JpaRepository<Product, Integer>{

    @Modifying
    @Query(value = "UPDATE Products p SET p.amount = :amount WHERE p.product_id= :id", nativeQuery = true)
    void updateProductAmount(@Param("amount") int amount, @Param("id") int id);

    public Product findById(int id);
}

and **Product: **

@Entity
@Table(name = "products")
public class Product {

    @Id
    @Column(name = "product_id")
    private int id;

    @Column(name = "description")
    @NotNull
    private String description;

    @Column(name = "amount")
    @NotNull
    private int amount;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
}

SQL if needed :

DROP TABLE IF EXISTS `products`;
CREATE TABLE `products` (
  `product_id` int(11) NOT NULL AUTO_INCREMENT,
  `description` varchar(255) NOT NULL,
  `amount` INT(64) NOT NULL,
  PRIMARY KEY (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=UTF8;
Leandro Rosa :

It seems like you are missing @Autowired annotation in the StoreController

@Controller
public class StoragePageController {

    @Autowired
    private StorageService storageService;

    @GET
    @RequestMapping(value = "/storage")
    public String openAllProductsPage(Model model) {

        List<Product> productList = storageService.findAll();
        model.addAttribute("productList", productList);
        return "storage";
    }
}

OR

The most recommended way to the dependency injection is to create a constructor with all dependencies.

@Controller
public class StoragePageController {


    private final StorageService storageService;

    public StoragePageController(StorageService storageService) {
       this.storageService = storageService;
    }

    @GET
    @RequestMapping(value = "/storage")
    public String openAllProductsPage(Model model) {

        List<Product> productList = storageService.findAll();
        model.addAttribute("productList", productList);
        return "storage";
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=389091&siteId=1