這是什麼導致null異常?我根本無法看到它..請第二套眼睛將幫助這個初學java學生..這個應用程序基本上捕捉到關於一個遊戲團隊的5個信息,並插入到MySQL數據庫。Java異常nullpointerexception錯誤
該錯誤是如下: 在線程異常 「AWT-EventQueue的-0」 顯示java.lang.NullPointerException在InsertTeamInfo.insertRecord(InsertTeamInfo.java:130)在InsertTeamInfo.access $ 100(InsertTeamInfo.java:11)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
public class InsertTeamInfo extends JFrame{
private String sql;
private Connection con;
private Statement stat;
private JButton btnSave;
private JTextField txtTeam;
private JTextField txtCity;
private JTextField txtYear;
private JTextField txtLoserTeam;
private JTextField txtLoserCity;
private JLabel lblTeam;
private JLabel lblCity;
private JLabel lblYear;
private JLabel lblLoserTeam;
private JLabel lblLoserCity;
public InsertTeamInfo(){
super("Favorite Team");
btnSave = new JButton("Save");
txtTeam = new JTextField("");
txtCity = new JTextField("");
txtYear = new JTextField("");
txtLoserTeam = new JTextField("");
txtLoserCity = new JTextField("");
lblTeam = new JLabel("Team");
lblCity = new JLabel("City");
lblYear = new JLabel("Year Played");
lblLoserTeam = new JLabel("Loser Team");
lblLoserCity = new JLabel("Loser City");
txtYear.setEditable(true);
txtLoserTeam.setEditable(true);
txtLoserCity.setEditable(true);
txtTeam.setEditable(true);
txtCity.setEditable(true);
}
public void launchJFrame(){
//width - height
setSize(500, 300);
getContentPane().setLayout(null);
getContentPane().setBackground(Color.white);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
getContentPane().add(btnSave);
getContentPane().add(txtTeam);
getContentPane().add(txtCity);
getContentPane().add(txtYear);
getContentPane().add(txtLoserTeam);
getContentPane().add(txtLoserCity);
getContentPane().add(lblTeam);
getContentPane().add(lblCity);
getContentPane().add(lblYear);
getContentPane().add(lblLoserTeam);
getContentPane().add(lblLoserCity);
lblTeam.setBounds(new Rectangle(65,20,100,30));
lblCity.setBounds(new Rectangle(65,55,100,30));
lblYear.setBounds(new Rectangle(65, 85, 100, 30));
lblLoserTeam.setBounds(new Rectangle(65, 115, 100, 30));
lblLoserCity.setBounds(new Rectangle(65, 145, 100, 30));
txtTeam.setBounds(new Rectangle(210, 20, 150, 30));
txtCity.setBounds(new Rectangle(210, 54, 150, 30));
txtYear.setBounds(new Rectangle(210, 86, 150, 30));
txtLoserTeam.setBounds(new Rectangle(210, 119, 150, 30));
txtLoserCity.setBounds(new Rectangle(210, 150, 150, 30));
btnSave.setBounds(new Rectangle(65, 190, 90, 30));
setVisible(true);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
shutDown();
}
});
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){ //Handle Save button click event
insertRecord();
}
});
}
private void insertRecord(){
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/week5", "root","Saras231");
}
catch(Exception ex){
JOptionPane.showMessageDialog(null, "Error connection to database.");
System.exit(0);
}
try {
sql = "INSERT INTO Wins_S001 (Team, City, Year_T, LoserTeam, LoserCity) VALUES (?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, txtTeam.getText());
preparedStatement.setString(2, txtCity.getText());
preparedStatement.setString(3, txtYear.getText());
preparedStatement.setString(4, txtLoserTeam.getText());
preparedStatement.setString(5, txtLoserCity.getText());
preparedStatement.executeUpdate();
JOptionPane.showMessageDialog(null, "Data Inserted Successfully");
}
catch(SQLException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Data Insert failed.");
}
try{
stat.close();
con.close();
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, "All active connection closed to the database.");
}
}
private void shutDown(){
int returnVal = JOptionPane.showConfirmDialog(this, "Are you sure you want to quit?");
if(returnVal == JOptionPane.YES_OPTION){
System.exit(0);
}
}
public static void main(String[] args){
InsertTeamInfo gui = new InsertTeamInfo();
gui.launchJFrame();
}
}
發佈堆棧跟蹤,包括它指示哪一行代碼導致問題的位置。然後讓我們知道您的代碼的哪一行與該行號相對應。 – MarsAtomic