2017-10-15 53 views
0

NOVICE這裏我得到一個錯誤「在線程異常‘的AWT - EventQueue的 - 0’顯示java.lang.NullPointerException」

我想找回我的數據庫中的字符串值,並將其轉換爲整數,這樣我當我按btnorder可以加整數。

這是代碼:

package jeriel.robie.zairhya.seki.blessie; 

import java.sql.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JOptionPane; 
import javax.swing.table.DefaultTableModel; 

public class Owner extends javax.swing.JFrame { 

public String dataBaseName= "feed"; 
public String dbURL="jdbc:mysql://localhost:3306/"+dataBaseName; 
public Connection con; 
public Statement s; 
public ResultSet rs1; 

public Owner() { 

    initComponents(); 

    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
    } catch (ClassNotFoundException ex) { 
     Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex); 
    } 

    try { 
     con=DriverManager.getConnection(dbURL,"root",""); 
    } catch (SQLException ex) { 
     Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

我認爲錯誤是在這裏的方法。我不知道它在哪裏。這是我第一次遇到這個錯誤。

public void orderitem() throws SQLException, ClassNotFoundException { 
    s = con.createStatement(); 
    ResultSet rs = s.getResultSet(); 
    String temp = rs.getString(3); 
    int x = Integer.parseInt(temp); 
    int y = x + Integer.parseInt(txtitemquantity.getText()); 
    s.execute("UPDATE stock SET itemquantity=" + y + "WHERE idnumber=" + "'" + 
    txtstockid.getText() + "'"); 
} 

private void btnorderActionPerformed(java.awt.event.ActionEvent evt) {           
    try { 
     orderitem(); 
    } catch (SQLException | ClassNotFoundException ex) { 
     Logger.getLogger(Owner.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

以下是錯誤

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
at jeriel.robie.zairhya.seki.blessie.Owner.orderitem(Owner.java:92) 
at jeriel.robie.zairhya.seki.blessie.Owner.btnorderActionPerformed(Owner.java:261) 
at jeriel.robie.zairhya.seki.blessie.Owner.access$500(Owner.java:17) 
at jeriel.robie.zairhya.seki.blessie.Owner$6.actionPerformed(Owner.java:168) 
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) 

回答

0

您沒有添加這樣可能要創建GUI組件你忘了初始化它們的代碼。假設txtstockid和txtitemquantity是組件,請確保您使用新的關鍵字創建它們。

+0

我在這裏使用netbeans。 –

+0

好的,IDE不重要,把更多的代碼與創建gui – niemar

相關問題