-1

I am trying to save data to database when clicking on the button submit and calling the NullPointerException error. When I tried to populate the database with a bean it went fine. But when I tried to save(code below) I got error. So any help?

public class InsertItem implements ActionListener {
    @Autowired
    private ProductService productService;
    static JTextField name,price,quantity;
    static JFrame f;
    static JButton submit;
    static JLabel nameLabel,priceLabel,quantityLabel;

    public void showInsertItem(){

        f = new JFrame("textfield");

        nameLabel = new JLabel("Product name:");
        priceLabel = new JLabel("Price:");
        quantityLabel = new JLabel("Quantity:");

        nameLabel.setLabelFor(name);
        priceLabel.setLabelFor(price);
        quantityLabel.setLabelFor(quantity);

        submit = new JButton("submit");
        submit.addActionListener(this);
        name = new JTextField(20);
        price = new JTextField(20);
        quantity = new JTextField(20);
        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
        p.add(nameLabel); p.add(name);p.add(new JSeparator());
        p.add(priceLabel);p.add(price);p.add(new JSeparator());
        p.add(quantityLabel);p.add(quantity);p.add(new JSeparator());
        p.add(submit);
        // add panel to frame
        f.add(p);
        // set the size of frame
        f.setSize(500, 300);
        f.setVisible(true);

    }
    public void actionPerformed(ActionEvent e){
        String s = e.getActionCommand();
        if (s.equals("submit")) {

            Product product = new Product( name.getText(), Integer.valueOf(price.getText()),Integer.valueOf(quantity.getText()));
            System.out.println(product.getId());
            System.out.println(product.getName());
            System.out.println(product.getPrice());
            System.out.println(product.getQuantity());
            productService.saveProduct(product);
            System.out.println("OK");
        }
    }

}
  • `@Component public interface ProductService { Product saveProduct(Product product); }` – Dat tran thanh Jun 02 '22 at 20:42
  • `@Service @Transactional public class ProductServiceImpl implements ProductService{ private final ProductRepo productRepo; public ProductServiceImpl(ProductRepo productRepo) { this.productRepo = productRepo; } @Override public Product saveProduct(Product product) { return productRepo.save(product); } } ` – Dat tran thanh Jun 02 '22 at 20:42
  • Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "com.example.demo.repo.service.ProductService.saveProduct(com.example.demo.entity.Product)" because "this.productService" is null at com.example.demo.menu.InsertItem.actionPerformed(InsertItem.java:76) at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1972) at – Dat tran thanh Jun 02 '22 at 20:43
  • See also [What is a stack trace, and how can I use it to debug my application errors?](https://stackoverflow.com/q/3988788/418556) – Andrew Thompson Jun 03 '22 at 03:45

0 Answers0