Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

I have nearly completed a Java CRUD app where a user store, delete, or update data from the MySQL workbench. The UI is completely visible once I run the app, but storing data does not function well as expected.


Here is the UI. Once I clicked the “Speichern” button, it is supposed to clear out all the text fields on this window and, to display what I entered on Java on SQL.

Right now clicking the button does not clear out the fields or display the transmitted data on SQL. I received the following error;

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at jcd_replicate.jcd_replicate_form$1.actionPerformed(jcd_replicate_form.java:106)
	

I am sure I am missing out small stuff to fix. I leave a little block of code for your reference, could anyone kindly point out where I could fix it?

speichernButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String Artikel, Preis, Anzahl;
                Artikel = textArtikel.getText();
                Preis = textPreis.getText();
                Anzahl = textAnzahl.getText();
                try  {
                    //Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/Artikelliste","root", "morry329A");
                    PreparedStatement pst = con.prepareStatement("INSERT INTO `Artikelliste.Artikeln`(`aName`, `Preis`, `Anzahl`)"
                            + "VALUES (?, ?, ?)");
                    ps.setString(1, Artikel);
                    ps.setString(2, Preis);
                    ps.setString(3, Anzahl);
                    ps.executeUpdate();
                    JOptionPane.showMessageDialog(null, "artikel hinzugefügt");
                    textArtikel.setText("");
                    textPreis.setText("");
                    textAnzahl.setText("");
                    textArtikel.requestFocus();
                } catch (SQLException exp){
                    exp.printStackTrace();
                }
            }
        });

What is line 106? The error code says the problem might be there.

Did you mean to leave the Connection con commented out? That would throw a null pointer exception when you call prepareStatement() on it

1 Like