-3

I don't know why I am having null despite comparing the code to the ones I've written before.

This is my Java code to insert data into table:

  @FXML
    private void rewards_Parameters(){
        try{
            String sql = "INSERT INTO SetUpReward VALUES(?,?,?,?,?,?)";
            if(sql !=null){
                pst =con.prepareStatement(sql);
                pst.setString(2, txt_minGiftValue.getText());
                pst.setString(3, txt_maxGiftValue.getText());
                pst.setString(4, txt_RewardPoint.getText());

                pst.execute();
            }
        }catch(SQLException e){
            JOptionPane.showMessageDialog(null, e);
        }    
    }

My Init process...

 /**
 * FXML Controller class
 *
 * @author JIDO
 */
public class IGisftCardController implements Initializable {
    Connection con =null;
    ResultSet rs = null;
  PreparedStatement pst ;

Stacktrace:

Caused by: 

java.lang.NullPointerException
    at view_controller.IGisftCardController.loadReward(IGisftCardController.java:249)
    at view_controller.IGisftCardController.initialize(IGisftCardController.java:168)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 22 more
Exception running application igiftcardfx.IGiftCardFX
Java Result: 1
olajide
  • 922
  • 1
  • 13
  • 23

1 Answers1

1

Because con is null. You test sql != null, make that con. And you should probably connect to a database (or at least log that you aren't). Also, please capture stack traces (they're invaluable for debugging your application). Something like,

String sql = "INSERT INTO SetUpReward VALUES(?,?,?,?,?,?)";
try {
    if (con != null) {
        pst = con.prepareStatement(sql);
        pst.setString(2, txt_minGiftValue.getText());
        pst.setString(3, txt_maxGiftValue.getText());
        pst.setString(4, txt_RewardPoint.getText());
        pst.setString(5, txt_CardNumber.getText());
        pst.setString(6, txt_PayPalEmail.getText());
        pst.setString(7, txt_BankName.getText());
        pst.setString(8, txt_BankAccount.getText());
        pst.execute();
    } else {
        System.out.println("No connection");
    }
} catch (SQLException e) {
    JOptionPane.showMessageDialog(null, e);
    e.printStackTrace();
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226