Java MySQL - Unknown Null Pointer Exception in simple MySQL Login Frame -
i have simple login screen connects mysql database. connects fine, throws null pointer exception when tries execute. here 2 snippets of code stack trace. bolded line exception being thrown.
package gui; import java.sql.*; import javax.swing.joptionpane; import java.awt.event.*; public class loginframe extends javax.swing.jframe { connection con; statement st; resultset rs; // login constructor public loginframe() { connect(); initcomponents(); setlocationrelativeto(null); } public void connect(){ try { con = drivermanager.getconnection( "jdbc:mysql://instance44668.db.xeround.com:5719/obliquedb?" + "user=name2&password=pass"); } catch (sqlexception ex) { system.err.println("cannot connect database server"); system.err.println(ex.getmessage()); } { try { con.close(); } catch (sqlexception ex) { system.out.println("better luck next time"); } } }
and here method executes statement
private void acceptbuttonactionperformed(java.awt.event.actionevent evt) { try { string user = usertextfield.gettext().trim(); char[] password = passwordtextfield.getpassword(); string pass = new string(password); string query = "select username,password users username = '"+user+"'and password = '"+pass+"'"; exception thrown-**rs = st.executequery(query);** int count = 0; while(rs.next()) { count++; } if(count == 1) { joptionpane.showmessagedialog(this, "login successful", "error message", joptionpane.error_message); } else if(count > 1) { joptionpane.showmessagedialog(this, "duplicate records found!", "error message", joptionpane.error_message); } else { joptionpane.showmessagedialog(this, "user not found", "error message", joptionpane.error_message); } } catch (exception e) { system.out.println("error retrieving records" + e); } }
and stack trace
error retrieving recordsjava.lang.nullpointerexception java.lang.nullpointerexception @ gui.loginframe.acceptbuttonactionperformed(loginframe.java:178) @ gui.loginframe.access$000(loginframe.java:7) @ gui.loginframe$1.actionperformed(loginframe.java:94) @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2018) @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2341) @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402) @ javax.swing.defaultbuttonmodel.setpressed(defaultbuttonmodel.java:259) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252) @ java.awt.component.processmouseevent(component.java:6505) @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3321) @ java.awt.component.processevent(component.java:6270) @ java.awt.container.processevent(container.java:2229) @ java.awt.component.dispatcheventimpl(component.java:4861) @ java.awt.container.dispatcheventimpl(container.java:2287) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4832) @ java.awt.lightweightdispatcher.processmouseevent(container.java:4492) @ java.awt.lightweightdispatcher.dispatchevent(container.java:4422) @ java.awt.container.dispatcheventimpl(container.java:2273) @ java.awt.window.dispatcheventimpl(window.java:2713) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:707) @ java.awt.eventqueue.access$000(eventqueue.java:101) @ java.awt.eventqueue$3.run(eventqueue.java:666) @ java.awt.eventqueue$3.run(eventqueue.java:664) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:87) @ java.awt.eventqueue$4.run(eventqueue.java:680) @ java.awt.eventqueue$4.run(eventqueue.java:678) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:677) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:211) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:128) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:117) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:113) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:105) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:90)
in connect
connection closed.
the statement better should preparedstatement (having miscellaneous advantages).
so create statement in connect , remove finally.
make disconnect
called on application exit closing statement , connection.
the result set rs
should close too.
string query = "select username,password " + "from users " + "where binary username = ? " + "and binary password = ?";
for case sensitive comparison, either specify binary collation columns, or use binary.
passwords can handled more appropiately, talking desk top application it's fine.
Comments
Post a Comment