0
我試圖將我的Tic-Tac-Toe服務器連接到數據庫以輸入玩家的詳細信息。我在服務器中創建了一個按鈕,在服務器正在運行時將按鈕添加到數據庫中。下面是我的代碼的摘錄和錯誤:Error in actionPerformed while using createStatement
public class TicTacToeServer extends JFrame {
Scanner scan = new Scanner(System.in);
String player1Name;
String player2Name;
static JTextArea jtaLog;
int winState = 0;
static JButton addPlayer;
static JButton delPlayer;
static JButton getResults;
static JButton delResults;
static JButton cumulativeResults;
public static int PLAYER1 = 1;
public static int PLAYER2 = 2;
public static int PLAYER1_WON = 1;
public static int PLAYER2_WON = 2;
public static int DRAW = 3;
public static int CONTINUE = 4;
private final String username = "user";
private final String password = "1234";
private Connection con;
private final String ID = "ID";
private final String Name = "Name";
private final String Surname = "Surname";
private final String Username = "Username";
private final String Password = "Password";
private final String Email = "Email";
static TicTacToeServer frame;
public static void main(String[] args) throws Exception {
frame = new TicTacToeServer();
frame.connectToDB();
}
public TicTacToeServer() throws SQLException {
jtaLog = new JTextArea();
JScrollPane scrollPane = new JScrollPane(jtaLog);
add(scrollPane, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 300);
setTitle("TicTacToeServer");
setVisible(true);
JPanel panel = new JPanel();
add(panel, BorderLayout.SOUTH);
addPlayer = new JButton("Add Player");
delPlayer = new JButton("Delete Player");
getResults = new JButton("Obtain Results");
delResults = new JButton("Delete Results");
cumulativeResults = new JButton("Obtain Cumulative Results");
panel.add(addPlayer);
panel.add(delPlayer);
panel.add(getResults);
panel.add(delResults);
panel.add(cumulativeResults);
addPlayer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent click) {
Object source = click.getSource();
if (source == addPlayer) {
System.out.println("Please provide unique ID for the user:");
int id = scan.nextInt();
System.out.println("Please type in the first name:");
String firstName = scan.next();
System.out.println("Please type in the surname:");
String surname = scan.next();
System.out.println("Please type in the username:");
String username = scan.next();
System.out.println("Please type in the password:");
String password = scan.next();
System.out.println("Please type in the email:");
String email = scan.next();
**try (Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE)) {**
ResultSet rs = stmt.executeQuery("SELECT * FROM PLAYERS");
rs.moveToInsertRow();
rs.updateInt(ID, id);
rs.updateString(Name, firstName);
rs.updateString(Surname, surname);
rs.updateString(Username, username);
rs.updateString(Password, password);
rs.updateString(Email, email);
rs.insertRow();
} catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}
});
delPlayer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent click) {
Object source = click.getSource();
if (source == delPlayer) {
System.out.println("Enter the user ID that should be removed:");
int id = scan.nextInt();
**try (Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE))** {
ResultSet rs = stmt.executeQuery("SELECT * FROM PLAYERS");
while (rs.next()) {
if (rs.getInt("ID") == id) {
rs.deleteRow();
}
}
} catch (SQLException err) {
System.out.println(err.getMessage());
}
}
}
});
//getResults.addActionListener(this);
//delResults.addActionListener(this);
//cumulativeResults.addActionListener(this);
public void connectToDB() throws SQLException {
DriverManager.getConnection("jdbc:derby://localhost:1527/Project", username, password);
}
}
正如代碼(**)強調,我的錯誤表明它有事情做這一行:
try (Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE))
這裏是我的錯誤信息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tictactoeserver.TicTacToeServer$1.actionPerformed(TicTacToeServer.java:91)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
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:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
比表示第錯誤行上述行其他(有關的actionPerformed,行91),我不知道如何去這一點。我如何否定這個錯誤?