0

NOVICE HERE

I want to retrieve the string value in my database and convert it to integer so that I can add integer when I press btnorder.

This is the code:

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);
    }
}   

I think the error is here in the method. I don't know where it is. This is my first to encounter this error.

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);
    }
} 

Here is the ERROR

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 Answers1

0

You didn't add code that create gui components so probably you forgot to initialize them. Assuming that txtstockid and txtitemquantity are components, make sure that you created them using new keyword.

niemar
  • 612
  • 1
  • 7
  • 16