0

this is my code an di get this error, I don't understand why, I tried multiple times to figure it out what is happening, are you able please to help me. I tried to check the several topic that are around here regarding this but nothing similar found and if I need to do a null check, I am new to this and I don't understand exactly what I need to do

import javax.swing.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;






import java.sql.*;






public class JavaCrud {
    
private JPanel Main;
   
 private JTextField txtName;

    private JButton saveButton;

    private JButton deleteButton;

    private JButton updateButton;

    private JTextField textField2;

    private JTextField txtPrice;

    private JTextField txtQty;



    public static void main(String[] args) {
        JFrame frame = new JFrame("JavaCrud");
        frame.setContentPane(new JavaCrud().Main);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

    public JavaCrud() {

        Connect();

        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name, price, qty;

                name = txtName.getText();
                price = txtPrice.getText();
                qty = txtQty.getText();

                try {
                    pst = con.prepareStatement("insert into products(pname,price,qty)values(?,?,?)");
                    pst.setString(1, name);
                    pst.setString(2, price);
                    pst.setString(3, qty);
                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null,"Record Addedddddd!!!!");

                    txtName.setText("");
                    txtPrice.setText("");
                    txtQty.setText("");
                    txtName.requestFocus();
                }

                catch (SQLException e1)
                {
                    e1.printStackTrace();
                }
            }
        });
        deleteButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String bid;

                bid = textField2.getText();


                try {
                    pst = con.prepareStatement("delete from products  where pid = ?");
                    pst.setString(1, bid);

                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Record Deleteeeeee!!!!!");

                    txtName.setText("");
                    txtPrice.setText("");
                    txtQty.setText("");
                    txtName.requestFocus();
                    textField2.setText("");
                }

                catch (SQLException e1)
                {

                    e1.printStackTrace();
                }
            }
        });
        updateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String pid,name,price,qty;

                name = txtName.getText();
                price = txtPrice.getText();
                qty = txtQty.getText();
                pid = textField2.getText();

                try {

                    pst = con.prepareStatement("update products set pname = ?,price = ?,qty = ? where pid = ?");
                    pst.setString(1, name);
                    pst.setString(2, price);
                    pst.setString(3, qty);
                    pst.setString(4, pid);

                    pst.executeUpdate();
                    JOptionPane.showMessageDialog(null, "Record Updateee!!!!!");

                    txtName.setText("");
                    txtPrice.setText("");
                    txtQty.setText("");
                    txtName.requestFocus();
                    textField2.setText("");
                }

                catch (SQLException e1)
                {

                    e1.printStackTrace();
                }
            }
        });
    }


    Connection con;
    PreparedStatement pst;
    public void Connect(){
        try{
            Connection con;
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/gbproducts","root","root");
            System.out.println("Success");
        }catch(SQLException ex){
            ex.printStackTrace();
        }

    }
}

1 Answers1

0

A NullPointerException is thrown because you adding an addActionListener to your button that has not been instantiated.

In your JavaCrud() method you need to instantiate your buttons.

saveButton = new JButton("SAVE"); 
deleteButton = new JButton("DELETE"); 
updateButton = new JButton("UPDATE");

You would also need to instantiate your JTextfield because after a user clicks on a button you are trying to getText() from a JTextField that is null.

Also, it is better to implements ActionListener to your class than to addActionListener to every button. It allows for cleaner code and easier debugging.

Here you can read up on ActionListener Interface

Karl H
  • 131
  • 1
  • 6