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