0
votes

I am trying to delete a row from my database and I am getting the following error

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

Also I have "where ID = 1"

Is it possible for me to have a getmethod to get the key(number) instead of having to type 1, 2 or 3 etc. a copy of the code is below

package prototype4;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DeleteSong extends JFrame implements ActionListener {

    private Connection con;
    private Statement stmt;
    private ResultSet res;
    JTextField songNo = new JTextField(7);
    TextArea information = new TextArea(2, 50);
    JButton delete = new JButton("Delete Song");
    JButton update = new JButton("Update Table");
    static String[] columnNames = {"trackNo", "Artist", "trackName"};
    static Object[][] data = null;
    static JTable table;
    static DefaultTableModel modeltable = new DefaultTableModel(data, columnNames);
    Font timeFont = new Font("Gill Sans MT", Font.BOLD, 15);

    //object constructor which delcares the physical look of the frame including the title
    public DeleteSong() {
        setLayout(new BorderLayout());
        setBounds(100, 100, 1000, 250); //set the size of the frame in pixels
        setTitle("Delete Song"); //set the title of the frame

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);


        JPanel top = new JPanel(); 
        top.add(new JLabel("Enter Song Number:"));
        top.add(songNo);
        top.add(delete); 
        delete.addActionListener(this); 
        top.add(update);
        update.addActionListener(this);
        add("North", top); 

        JPanel middle = new JPanel(); line
        middle.add(information); 
        add("Center", middle);

        JPanel panel2 = new JPanel();

        table = new JTable(modeltable);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);

        panel2.add(scrollPane);
        add("West", panel2);

        setResizable(false); 
        setVisible(true);

        setLocationRelativeTo(null);

        information.setFont(timeFont);
        information.setForeground(Color.BLUE);
    }

    public void actionPerformed(ActionEvent e) {
        try {
            stmt = con.createStatement();
            res = stmt.executeQuery("SELECT * FROM LibraryTable");
            String key = songNo.getText(); //get the song number from the songNo text field 
            String name = LibraryData.getName(key); //get the name of the song from the LibraryData.java(another java class)
            //conditions
            if (name == null) {
                information.setText("No such song");
            } else {
                int confirm = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + key + " " + name.toUpperCase() + " ?");
                if (confirm == 0) {
                    LibraryData.remove(key);
                    stmt = con.createStatement();
                    String sql = "DELETE FROM LibraryTable "
                            + "WHERE ID = 1";
                    stmt.executeUpdate(sql);
                    information.setText(key + "-" + name + " has been deleted and database updated!");
                }
            }
        } catch (SQLException ex) {
            Logger.getLogger(DeleteSong.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at prototype4.DeleteSong.actionPerformed(DeleteSong.java:92) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6505) at javax.swing.JComponent.processMouseEvent(JComponent.java:3321) at java.awt.Component.processEvent(Component.java:6270) at java.awt.Container.processEvent(Container.java:2229) at java.awt.Component.dispatchEventImpl(Component.java:4861) at java.awt.Container.dispatchEventImpl(Container.java:2287) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) at java.awt.Container.dispatchEventImpl(Container.java:2273) at java.awt.Window.dispatchEventImpl(Window.java:2719) at java.awt.Component.dispatchEvent(Component.java:4687) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:694) at java.awt.EventQueue$3.run(EventQueue.java:692) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) at java.awt.EventQueue$4.run(EventQueue.java:708) at java.awt.EventQueue$4.run(EventQueue.java:706) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) Picked up JAVA_TOOL_OPTIONS: -Djava.vendor="New Oracle"

1
The stack trace of the exception tells you exactly where it happens. Something is null at this line. - JB Nizet
Could you post the entire stack trace? It will tell you where the exception is occurring. - JamesB
You should initialise the connection 'con'. Also you should close connections and statements. - Petros Tsialiamanis

1 Answers

2
votes

You haven't initialized the con object variable. you need to do

con =  DriverManager.getConnection(url);

Please refer to this for more details.