2014-07-07 146 views
0

這是什麼導致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(); 
    } 
} 
+6

發佈堆棧跟蹤,包括它指示哪一行代碼導致問題的位置。然後讓我們知道您的代碼的哪一行與該行號相對應。 – MarsAtomic

回答

1

刪除stat實例變量。刪除con實例變量。刪除sql實例變量。他們應該都是本地變量(這可能是你的例外的原因:你正在關閉stat而不是關閉preparedStatement。堆棧跟蹤中的行號應該確認它)。

此外,請確保它們在finally塊中關閉,以確保它們已關閉。請注意:如果關閉準備好的語句會引發異常,則連接將不會關閉。

+0

非常感謝!我遵循了你的所有建議,並處理好了。 – Athapali

+0

下一次,仔細閱讀異常堆棧跟蹤:行號告訴你拋出異常的位置,這使得找到問題成爲一項三重任務。另外,在發佈堆棧跟蹤時,告訴我們它指的是哪個行號。 –

相關問題