java - NulPointerException with TextFields -
when try value of textfield , use in class nullpointer exception..
exception in thread "awt-eventqueue-0" java.lang.nullpointerexception @ gui.addnewpasswordwindow.actionperformed(addnewpasswordwindow.java:86) @ javax.swing.abstractbutton.fireactionperformed(unknown source) @ javax.swing.abstractbutton$handler.actionperformed(unknown source) @ javax.swing.defaultbuttonmodel.fireactionperformed(unknown source) @ javax.swing.defaultbuttonmodel.setpressed(unknown source) @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(unknown source) @ java.awt.component.processmouseevent(unknown source) @ javax.swing.jcomponent.processmouseevent(unknown source) @ java.awt.component.processevent(unknown source) @ java.awt.container.processevent(unknown source) @ java.awt.component.dispatcheventimpl(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.lightweightdispatcher.retargetmouseevent(unknown source) @ java.awt.lightweightdispatcher.processmouseevent(unknown source) @ java.awt.lightweightdispatcher.dispatchevent(unknown source) @ java.awt.container.dispatcheventimpl(unknown source) @ java.awt.window.dispatcheventimpl(unknown source) @ java.awt.component.dispatchevent(unknown source) @ java.awt.eventqueue.dispatcheventimpl(unknown source) @ java.awt.eventqueue.access$500(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.awt.eventqueue$3.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.security.protectiondomain$1.dointersectionprivilege(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.awt.eventqueue$4.run(unknown source) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$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)
this class error in, when click add button
public class addnewpasswordwindow implements actionlistener{ static passwordstoragewindow passwordstoragewindow; private jframe frame; private jtextfield passwordfield; private jtextfield usernamefield; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { addnewpasswordwindow window = new addnewpasswordwindow(passwordstoragewindow); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } public addnewpasswordwindow(passwordstoragewindow passwordstoragewindow) { this.passwordstoragewindow = passwordstoragewindow; initialize(); } private void initialize() { frame = new jframe(); frame.setalwaysontop(true); frame.setbounds(100, 100, 240, 240); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().setlayout(null); jlabel lblusername = new jlabel("username:"); lblusername.setfont(new font("tahoma", font.plain, 14)); lblusername.setbounds(10, 11, 204, 20); frame.getcontentpane().add(lblusername); jlabel lblpassword = new jlabel("password:"); lblpassword.setfont(new font("tahoma", font.plain, 14)); lblpassword.setbounds(10, 81, 143, 20); frame.getcontentpane().add(lblpassword); jbutton btnadd = new jbutton("add"); btnadd.setbounds(64, 160, 89, 23); frame.getcontentpane().add(btnadd); btnadd.addactionlistener(this); btnadd.setactioncommand("add"); jbutton btngenerate = new jbutton("generate"); btngenerate.setbounds(125, 82, 89, 23); frame.getcontentpane().add(btngenerate); btngenerate.addactionlistener(this); btngenerate.setactioncommand("generate"); usernamefield = new jtextfield(); usernamefield.setbounds(30, 42, 184, 20); frame.getcontentpane().add(usernamefield); usernamefield.setcolumns(10); passwordfield = new jtextfield(); passwordfield.setbounds(30, 116, 184, 20); frame.getcontentpane().add(passwordfield); passwordfield.setcolumns(10); } public void actionperformed(actionevent e) { if("generate".equals(e.getactioncommand())){ random generator = new random(); passwordfield.settext(generator.tostring()); } if("add".equals(e.getactioncommand())){ string username = usernamefield.gettext(); string password = passwordfield.gettext(); passwordstoragewindow.getusername(username); passwordstoragewindow.getpassword(password); } } }
and class want value of text box stored , shown...
public class passwordstoragewindow implements actionlistener{ private jframe frmexistingpassword; private jtable table; public arraylist<string> data1; string username = loginwindow.username(); public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { passwordstoragewindow passwordstoragewindow = new passwordstoragewindow(); passwordstoragewindow.frmexistingpassword.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); } public void getusername(string username){ data1.add(username); } public void getpassword(string password){ data1.add(password); } public passwordstoragewindow() throws filenotfoundexception { initialize(); } private void initialize() throws filenotfoundexception { string[] columnnames = {"site", "password"}; file f = new file("d:\\programming\\projects\\workspace\\password storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt"); scanner scan = new scanner(f); data1 = new arraylist<string>(); while (scan.hasnextline()) { data1.add(scan.nextline()); } object[][] data = new object[data1.size()/2+1][2]; data[0][0] = "site"; data[0][1] = "password"; int counter = 1; (int = 0; < data1.size(); i+=2){ data[counter][0] = data1.get(i); data[counter][1] = data1.get(i+1); counter++; } frmexistingpassword = new jframe(); frmexistingpassword.settitle("existing passwords"); frmexistingpassword.setbounds(100, 100, 560, 600); frmexistingpassword.setresizable(false); frmexistingpassword.setdefaultcloseoperation(jframe.exit_on_close); frmexistingpassword.getcontentpane().setlayout(null); imageicon add = new imageicon("add.png"); imageicon delete = new imageicon("delete.png"); jbutton buttonadd = new jbutton("add", add); buttonadd.setbounds(162, 4, 180, 75); frmexistingpassword.getcontentpane().add(buttonadd); buttonadd.setactioncommand("add"); jbutton buttondelete = new jbutton("delete password", delete); buttondelete.setbounds(360, 4, 180, 75); frmexistingpassword.getcontentpane().add(buttondelete); buttondelete.setactioncommand("delete"); jbutton buttonback = new jbutton("go back"); buttonback.setbounds(5, 4, 140, 75); frmexistingpassword.getcontentpane().add(buttonback); buttonback.setactioncommand("back"); table = new jtable(data, columnnames) { public boolean iscelleditable(int row, int col) { return false; } }; table.setbounds(2, 106, 542, 455); frmexistingpassword.getcontentpane().add(table); table.setpreferredscrollableviewportsize(new dimension(520, 500)); table.setfillsviewportheight(true); buttonadd.addactionlistener(this); buttondelete.addactionlistener(this); buttonback.addactionlistener(this); } public void actionperformed(actionevent e) { if("back".equals(e.getactioncommand())){ mainmenuwindow.main(null); frmexistingpassword.dispose(); } if("add".equals(e.getactioncommand())){ file file = new file("d:\\programming\\projects\\workspace\\password storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt"); if(!file.exists()) { try { printwriter out = new printwriter(file); out.close(); } catch (filenotfoundexception e1) {} } addnewpasswordwindow newwindow = new addnewpasswordwindow(null); addnewpasswordwindow.main(null); } try { printwriter out = new printwriter("d:\\programming\\projects\\workspace\\password storage v1.1\\usernames\\" + username + "\\" + username + ".pass.txt"); (int = 0; i<data1.size(); i++) { out.println(data1.get(i)); } out.close(); passwordstoragewindow frmexistingpassword1 = new passwordstoragewindow(); frmexistingpassword1.main(null); frmexistingpassword.setdefaultcloseoperation(jframe.dispose_on_close); frmexistingpassword.setvisible(false); } catch (exception e1) { system.out.println("error writing new file/saving changes...!"); } } }
without telling line 86, seems me never initialize passwordstoragewindow
.
you have:
static passwordstoragewindow passwordstoragewindow; // not initialized anywhere private jframe frame; private jtextfield passwordfield; private jtextfield usernamefield; public static void main(string[] args) { eventqueue.invokelater(new runnable() { public void run() { try { // passing null value here, passwordstoragewindow not initialized. addnewpasswordwindow window = new addnewpasswordwindow(passwordstoragewindow); window.frame.setvisible(true); } catch (exception e) { e.printstacktrace(); } } }); }
check comments in code.
on side note, should follow java convention , use camelcase, variables , method names should start lower case.
Comments
Post a Comment