java - How to get rid of the nullpointerexception in jframe? -
my intention create login page using jframe. have created database connection class in separate file below , when run login jframe, error saying nullpointer exception.
please assist me :)
dbconnection class ----------------------------------------------
package vehicle_renting; import java.sql.*; import javax.swing.joptionpane; public class dbconnection { connection con; statement stmt; resultset rs; public dbconnection() {} public void connect() { try { class.forname("com.jdbc.mysql.driver"); con=drivermanager.getconnection("jdbc:mysql://localhost:3306/vehicle_renting_1","root","qwer1234"); } catch (exception e) { e.printstacktrace(); } } }
and below code placed in jframe source.
package vehicle_renting; import java.sql.* ; import javax.swing.* ; public class login extends javax.swing.jframe { connection conn ; resultset rs; preparedstatement pst; /** * creates new form login */ public login() { initcomponents(); } /** * method called within constructor initialize form. * warning: not modify code. content of method * regenerated form editor. */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code"> private void initcomponents() { jlabel1 = new javax.swing.jlabel(); jlabel2 = new javax.swing.jlabel(); jbutton1 = new javax.swing.jbutton(); txt_username = new javax.swing.jtextfield(); txt_password = new javax.swing.jpasswordfield(); setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close); jlabel1.settext("username"); jlabel2.settext("password"); jbutton1.settext("ok"); jbutton1.addactionlistener(new java.awt.event.actionlistener() { public void actionperformed(java.awt.event.actionevent evt) { jbutton1actionperformed(evt); } }); javax.swing.grouplayout layout = new javax.swing.grouplayout(getcontentpane()); getcontentpane().setlayout(layout); layout.sethorizontalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(110, 110, 110) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false) .addgroup(layout.createsequentialgroup() .addcomponent(jlabel2) .addgap(18, 18, 18) .addcomponent(txt_password)) .addgroup(layout.createsequentialgroup() .addcomponent(jlabel1) .addgap(18, 18, 18) .addcomponent(txt_username, javax.swing.grouplayout.preferred_size, 180, javax.swing.grouplayout.preferred_size))) .addcontainergap(44, short.max_value)) .addgroup(javax.swing.grouplayout.alignment.trailing, layout.createsequentialgroup() .addcontainergap(javax.swing.grouplayout.default_size, short.max_value) .addcomponent(jbutton1) .addgap(143, 143, 143)) ); layout.setverticalgroup( layout.createparallelgroup(javax.swing.grouplayout.alignment.leading) .addgroup(layout.createsequentialgroup() .addgap(60, 60, 60) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel1) .addcomponent(txt_username, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(18, 18, 18) .addgroup(layout.createparallelgroup(javax.swing.grouplayout.alignment.baseline) .addcomponent(jlabel2) .addcomponent(txt_password, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)) .addgap(18, 18, 18) .addcomponent(jbutton1) .addcontainergap(141, short.max_value)) ); pack(); }// </editor-fold> private void jbutton1actionperformed(java.awt.event.actionevent evt) { // todo add handling code here: string username=txt_username.gettext(); string password=txt_password.gettext(); string sql = "select * login username = ? , password = ?"; try { pst = conn.preparestatement(sql); pst.setstring(1, username); // passing values username textbox pst.setstring(2, password); // passing values password textbox rs = pst.executequery(); // storing results retrieved query if (rs.next()) { joptionpane.showmessagedialog(null, "username , password correct"); } else { joptionpane.showmessagedialog(null, "invalid username , password"); } } catch (exception e) { joptionpane.showmessagedialog(null, e); } } /** * @param args command line arguments */ public static void main(string args[]) { /* set nimbus , feel */ //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) "> /* if nimbus (introduced in java se 6) not available, stay default , feel. * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) { if ("nimbus".equals(info.getname())) { javax.swing.uimanager.setlookandfeel(info.getclassname()); break; } } } catch (classnotfoundexception ex) { java.util.logging.logger.getlogger(login.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (instantiationexception ex) { java.util.logging.logger.getlogger(login.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (illegalaccessexception ex) { java.util.logging.logger.getlogger(login.class.getname()).log(java.util.logging.level.severe, null, ex); } catch (javax.swing.unsupportedlookandfeelexception ex) { java.util.logging.logger.getlogger(login.class.getname()).log(java.util.logging.level.severe, null, ex); } //</editor-fold> /* create , display form */ java.awt.eventqueue.invokelater(new runnable() { public void run() { new login().setvisible(true); } }); } // variables declaration - not modify private javax.swing.jbutton jbutton1; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jpasswordfield txt_password; private javax.swing.jtextfield txt_username; // end of variables declaration }
and when run jframe, says "java.lang.nullpointerexception" . can't find error in this. thank you
try { pst = conn.preparestatement(sql); pst.setstring(1, username); // passing values username textbox pst.setstring(2, password); // passing values password textbox rs = pst.executequery(); // storing results retrieved query if (rs.next()) { joptionpane.showmessagedialog(null, "username , password correct"); } else { joptionpane.showmessagedialog(null, "invalid username , password"); } } catch (exception e) { joptionpane.showmessagedialog(null, e); }
i think above code segment gives me error. had edit joptionpane find out.
i'd haven't initialized property conn
in jframe code
Comments
Post a Comment