Java/MySQL Prepared Statement Error -


this question has answer here:

before mark duplicate, please note had @ thread null pointer exception have on stackoverflow. unfortunately, have not managed solve problem , grateful if point me in right direction, since stuck quite while now.

i getting error

java.lang.nullpointerexception @ sample.loginwindow.setbtnlogin(loginwindow.java:39)) point @ following line preparedstatement.setstring(1, txtusername.gettext()

i have class dbutilities holds database connection , closing methods:

 public class dbutilities {     // getting connection database.     public static connection getconnection() throws sqlexception {         string url = "jdbc:mysql://localhost:3306/studentmanagementdb";         string user = "admin";         string pass = "administrator";         connection connection = drivermanager.getconnection(url, user, pass);         return connection;     }     // close prepared statement.     public static void closepreparedstatement(preparedstatement pstmt) {         try {             if(pstmt != null) {                 pstmt.close();             }         }catch(sqlexception sexepction) {             showerrormsg("sql database error:", "prepared statement not closed.");         }     }     // close result set.     public static void closeresultset(resultset resset) {         try {             if (resset != null){                 resset.close();             }         }catch (sqlexception sexception) {             showerrormsg("sql database error:", "result set not closed.");         }     }     public static void closeconnection(connection connect) {         try {             if(connect != null) {                 connect.close();             }         }catch (sqlexception sexception) {             showerrormsg("sql database error:", "database connection not close.");         }     }     // shows error message.     public static void showerrormsg(string title, string msg) {         platform.runlater(() -> {             alert alert = new alert(alert.alerttype.error);             alert.settitle(title);             alert.setheadertext(msg);             alert.setwidth(200);             alert.setheight(200);             alert.showandwait();         });     } } 

class uses connection:

public class loginwindow implements initializable{      @fxml     private textfield txtusername;     @fxml     private passwordfield txtpassword;     @fxml     private button btnlogin;      connection connection = null;     preparedstatement preparedstatement = null;     resultset resultset = null;     dbutilities dbutil =  new dbutilities();        // setting login button.     @fxml     private void setbtnlogin(actionevent event) {          try {             connection = dbutil.getconnection();             string sqlquery = "select * 'user_login_details' 'user_name' = ? , 'user_password' = ?";             connection.preparestatement(sqlquery);             preparedstatement.setstring(1, txtusername.gettext());             preparedstatement.setstring(2, txtpassword.gettext());             resultset = preparedstatement.executequery();          }catch (exception exception) {             //dbutilities.showerrormsg("database connection error:", "could not connect database.");             exception.printstacktrace();         }finally {             dbutil.closepreparedstatement(preparedstatement);             dbutil.closeresultset(resultset);             dbutil.closeconnection(connection);         }     }     @override     public void initialize(url location, resourcebundle resources) {         btnlogin.setonaction(this::setbtnlogin);     } } 

preparedstatement = connection.preparestatement(sqlquery); 

creates preparedstatement object sending parameterized sql statements database.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -