我收到错误"线程中的异常" AWT-EventQueue-0"显示java.lang.NullPointerException"

时间:2017-10-15 08:46:59

标签: java

NOVICE HERE

我想检索数据库中的字符串值并将其转换为整数,以便在按 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)

1 个答案:

答案 0 :(得分:0)

您没有添加创建gui组件的代码,因此您可能忘记初始化它们。假设txtstockid和txtitemquantity是组件,请确保使用new关键字创建它们。