java - i cant adding item to my jcombobox from my database -
i've been trying hours insert data jcombobox , frustates me can please me find error
here's code
import java.awt.borderlayout; import java.awt.eventqueue; import javax.swing.defaultlistmodel; import javax.swing.jframe; import javax.swing.joptionpane; import javax.swing.jpanel; import javax.swing.border.emptyborder; import javax.swing.jlabel; import javax.swing.jbutton; import java.awt.gridlayout; import java.awt.font; import java.sql.sqlexception; import java.util.vector; import javax.swing.jlist; import javax.swing.jtextfield; import javax.swing.jcombobox; import java.awt.event.itemlistener; import java.awt.event.itemevent; import java.awt.event.actionlistener; import java.awt.event.actionevent; public class staffeditdelete extends jframe { private jpanel contentpane; private jtextfield textfield; private jtextfield textfield_1; private jtextfield textfield_2; private jtextfield textfield_3; private jtextfield textfield_4; private jtextfield textfield_5; /** * launch application. */ public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { staffeditdelete frame = new staffeditdelete(); frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } /** * create frame. */ connect con; // string []alpha; vector<string> alpha; jcombobox combobox; public staffeditdelete() { settitle("staff"); setdefaultcloseoperation(jframe.dispose_on_close); setbounds(100, 100, 450, 424); contentpane = new jpanel(); contentpane.setborder(new emptyborder(5, 5, 5, 5)); setcontentpane(contentpane); contentpane.setlayout(new borderlayout(0, 0)); jpanel panel = new jpanel(); contentpane.add(panel, borderlayout.north); jlabel lblstaff = new jlabel("staff"); lblstaff.setfont(new font("times new roman", font.bold, 24)); panel.add(lblstaff); jpanel panel_1 = new jpanel(); contentpane.add(panel_1, borderlayout.center); panel_1.setlayout(new gridlayout(7, 2, 10, 10)); jlabel lblnewlabel = new jlabel("id"); panel_1.add(lblnewlabel); con.rs = con.executequery("select * detail"); try { while(con.rs.next()) { // system.out.println("true"); combobox.additem(con.rs.getstring(1)); } } catch (exception e2) { // todo auto-generated catch block e2.printstacktrace(); } combobox = new jcombobox(); combobox.additemlistener(new itemlistener() { public void itemstatechanged(itemevent e) { string items = e.getsource().tostring(); con.rs = con.executequery("select * staff [id staff ='"+items+"']"); try { textfield.settext(con.rs.getstring(2)); textfield_1.settext(con.rs.getstring(3)); textfield_2.settext(con.rs.getstring(4)); textfield_3.settext(con.rs.getstring(5)); textfield_4.settext(con.rs.getstring(6)); textfield_5.settext(con.rs.getstring(7)); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } }); panel_1.add(combobox); jlabel lblnewlabel_1 = new jlabel("nama"); panel_1.add(lblnewlabel_1); textfield = new jtextfield(); panel_1.add(textfield); textfield.setcolumns(10); jlabel lblnewlabel_2 = new jlabel("alamat"); panel_1.add(lblnewlabel_2); textfield_1 = new jtextfield(); panel_1.add(textfield_1); textfield_1.setcolumns(10); jlabel lblnewlabel_3 = new jlabel("nomor telepon"); panel_1.add(lblnewlabel_3); textfield_2 = new jtextfield(); panel_1.add(textfield_2); textfield_2.setcolumns(10); jlabel lblnewlabel_4 = new jlabel("jabatan"); panel_1.add(lblnewlabel_4); textfield_3 = new jtextfield(); panel_1.add(textfield_3); textfield_3.setcolumns(10); jlabel lblnewlabel_5 = new jlabel("password"); panel_1.add(lblnewlabel_5); textfield_4 = new jtextfield(); panel_1.add(textfield_4); textfield_4.setcolumns(10); jlabel lblnewlabel_6 = new jlabel("gaji"); panel_1.add(lblnewlabel_6); textfield_5 = new jtextfield(); panel_1.add(textfield_5); textfield_5.setcolumns(10); jpanel panel_2 = new jpanel(); contentpane.add(panel_2, borderlayout.south); jbutton btnedit = new jbutton("edit"); btnedit.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { if (cektelpon()) { int reply = joptionpane.showconfirmdialog(null, "edit "+combobox.getselecteditem().tostring()+"?","edit?",joptionpane.yes_no_option); if (reply == joptionpane.yes_option) { con.executeupdatestaff(combobox.getselecteditem().tostring(), textfield.gettext(), textfield_1.gettext(), textfield_2.gettext(), textfield_3.gettext(), textfield_4.gettext(), textfield_5.gettext()); joptionpane.showmessagedialog(null, "data berhasil di edit"); } } else { joptionpane.showmessagedialog(null, "nomor telepon harus angka", "error", joptionpane.error_message); } } }); panel_2.add(btnedit); jbutton btndelete = new jbutton("delete"); btndelete.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { int reply = joptionpane.showconfirmdialog(null, "delete "+combobox.getselecteditem().tostring()+"?","delete?",joptionpane.yes_no_option); if(reply == joptionpane.yes_option) { con.executedeletestaff(combobox.getselecteditem().tostring()); joptionpane.showmessagedialog(null, "data berhasil di delete"); } } }); panel_2.add(btndelete); jbutton btnreset = new jbutton("reset"); btnreset.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { combobox.setselectedindex(0); con.rs = con.executequery("select * staff [id staff] = '"+ combobox.getselecteditem().tostring()+"'"); try { textfield.settext(con.rs.getstring(2)); textfield_1.settext(con.rs.getstring(3)); textfield_2.settext(con.rs.getstring(4)); textfield_3.settext(con.rs.getstring(5)); textfield_4.settext(con.rs.getstring(6)); textfield_5.settext(con.rs.getstring(7)); } catch (sqlexception e1) { // todo auto-generated catch block e1.printstacktrace(); } } }); panel_2.add(btnreset); jbutton btncancel = new jbutton("cancel"); btncancel.addactionlistener(new actionlistener() { public void actionperformed(actionevent e) { dispose(); } }); panel_2.add(btncancel); } public boolean cektelpon() { int flag = 0; (int =0 ;i < textfield_2.gettext().length();i++) { if (textfield_2.gettext().charat(i) == '0') { flag ++; } if (textfield_2.gettext().charat(i) == '1') { flag ++; } if (textfield_2.gettext().charat(i) == '2') { flag ++; } if (textfield_2.gettext().charat(i) == '3') { flag ++; } if (textfield_2.gettext().charat(i) == '4') { flag ++; } if (textfield_2.gettext().charat(i) == '5') { flag ++; } if (textfield_2.gettext().charat(i) == '6') { flag ++; } if (textfield_2.gettext().charat(i) == '7') { flag ++; } if (textfield_2.gettext().charat(i) == '8') { flag ++; } if (textfield_2.gettext().charat(i) == '9') { flag ++; } } if (flag == 0) { return true; } else { return false; } } }
here's error
java.lang.nullpointerexception @ staffeditdelete.<init>(staffeditdelete.java:85) @ staffeditdelete$1.run(staffeditdelete.java:44) @ java.awt.event.invocationevent.dispatch(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$000(unknown source) @ java.awt.eventqueue$1.run(unknown source) @ java.awt.eventqueue$1.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.accesscontrolcontext$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue.dispatchevent(unknown source) @ java.awt.eventdispatchthread.pumponeeventforfilters(unknown source) @ java.awt.eventdispatchthread.pumpeventsforfilter(unknown source) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.pumpevents(unknown source) @ java.awt.eventdispatchthread.run(unknown source)
ive trying hours please me find error in code
java.lang.nullpointerexception
at staffeditdelete.(staffeditdelete.java:85)
you initializing jcombobox
after combobox
object using. statement should before while
loop using object.
combobox = new jcombobox(); //should before block try { while(con.rs.next()) { // system.out.println("true"); //here using combobox. should initialized before statement. combobox.additem(con.rs.getstring(1)); } } catch (exception e2) { // todo auto-generated catch block e2.printstacktrace(); }//not after block
and 1 more problem con
object not created yet. check 1 also.
just @ statement.
con.rs = con.executequery("select * detail");
what con
, rs
here.
Comments
Post a Comment