我正在尝试从我的数据库中删除一行,但我收到以下错误
线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException
我还有"where ID = 1"
我是否可以使用getmethod来获取密钥(数字),而不必输入1,2或3等等。代码的副本在下面
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);
}
}
}
线程“AWT-EventQueue-0”中的异常java.lang.NullPointerException 在prototype4.DeleteSong.actionPerformed(DeleteSong.java:92)at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018) 在 javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2341) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在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) 在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 在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 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) 在 java.security.ProtectionDomain $ 1.doIntersectionPrivilege(ProtectionDomain.java:87) 在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) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:705)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 拿起JAVA_TOOL_OPTIONS:-Djava.vendor =“新甲骨文”