i building application verifies logins amazon web service s3 file, can verify login fine, issue arises when try text entered user in textfield.
this section of code builds ui :
public class userinterface extends jframe implements actionlistener { jlayeredpane pane; jframe f; jtextfield usernamelogin; jtextfield passwordlogin; userinterface ui; jbutton loginbutton; static string loggedinas = null; static amazons3 s3; static boolean trytologin = false; public userinterface() { jlayeredpane pane = new jlayeredpane(); jtextfield usernamelogin = new jtextfield("username...",20); usernamelogin.setlocation(650,200); usernamelogin.setsize(500, 30); usernamelogin.setvisible(true); pane.add(usernamelogin, 1, 0); jtextfield passwordlogin = new jtextfield("password...",20); passwordlogin.setlocation(650,240); passwordlogin.setsize(500, 30); passwordlogin.setvisible(true); pane.add(passwordlogin, 1, 0); jbutton loginbutton = new jbutton("login"); loginbutton.setlocation(650,290); loginbutton.setsize(75, 20); loginbutton.addactionlistener(this); loginbutton.setvisible(true); pane.add(loginbutton, 1, 0); this.add(pane); }
i dont think problom comes there, wrong. next part of program logic processor works server verify login amazon s3. in main().
int infinite = 1; try { system.out.println("downloading object"); s3object object = s3.getobject(new getobjectrequest("saucymmo", "logins.txt")); system.out.println("content-type: " + object.getobjectmetadata().getcontenttype()); while (infinite == 1) { system.out.println("ran"); if (trytologin == true) { system.out.println("ran2"); infinite = 0; bufferedreader br = new bufferedreader(new inputstreamreader(object.getobjectcontent())); string linevalue = null; while((linevalue = br.readline()) != null && loggedinas == null){ string splitresult[] = linevalue.split(","); boolean retval = splitresult[0].equals(ui.usernamelogin.gettext()); boolean retval2 = splitresult[1].equals(ui.passwordlogin.gettext()); if (retval == true && retval2 == true) { loggedinas = splitresult[0]; system.out.println("logged in : " + loggedinas); } else { system.out.println("split 0 : " + splitresult[0]); system.out.println("split 1 : " + splitresult[1]); } } } } } catch (amazonserviceexception ase) { } catch (amazonclientexception ace) { } catch (ioexception e) { e.printstacktrace(); }
i null pointer exception when call "ui.usernamelogin.gettext()" or "ui.passwordlogin.gettext()".
specifically,
java.lang.nullpointerexception @ userinterface.main(userinterface.java:102)
you defining local variable here in constructor
jtextfield usernamelogin = new jtextfield("username...",20);
that different jtextfield usernamelogin;
declaration never initialized , null
that's why have nullpointerexception
.
change
public class userinterface extends jframe implements actionlistener { jlayeredpane pane; jframe f; jtextfield usernamelogin; jtextfield passwordlogin; userinterface ui; jbutton loginbutton; public userinterface() { layeredpane pane = new jlayeredpane(); jtextfield usernamelogin = new jtextfield("username...",20); usernamelogin.setlocation(650,200); usernamelogin.setsize(500, 30); usernamelogin.setvisible(true); pane.add(usernamelogin, 1, 0); jtextfield passwordlogin = new jtextfield("password...",20); passwordlogin.setlocation(650,240); passwordlogin.setsize(500, 30); passwordlogin.setvisible(true); pane.add(passwordlogin, 1, 0); jbutton loginbutton = new jbutton("login"); loginbutton.setlocation(650,290); loginbutton.setsize(75, 20); loginbutton.addactionlistener(this); loginbutton.setvisible(true); pane.add(loginbutton, 1, 0); this.add(pane); }
to
public class userinterface { private jlayeredpane pane; private jtextfield usernamelogin; private jtextfield passwordlogin; private jbutton loginbutton; private jtextfield usernamelogin; private jframe frame; public userinterface() { usernamelogin = new jtextfield("username...",20); usernamelogin.setlocation(650,200); usernamelogin.setsize(500, 30); usernamelogin.setvisible(true); pane.add(usernamelogin, 1, 0); passwordlogin = new jtextfield("password...",20); passwordlogin.setlocation(650,240); passwordlogin.setsize(500, 30); passwordlogin.setvisible(true); pane.add(passwordlogin, 1, 0); loginbutton = new jbutton("login"); loginbutton.setlocation(650,290); loginbutton.setsize(75, 20); loginbutton.addactionlistener(this); loginbutton.setvisible(true); pane.add(loginbutton, 1, 0); frame= new jframe(); //configure jframe frame.add(pane); frame.pack();//size frame frame.setvisible(boolean.true); } //you have added component private class myactionlistener implements actionlistener{ @override public void actionperformed(actionevent evt){ //code here } } }
a few advices :
don't extends jframe
have reference instead (composition on inheritance) , don't implement in top classes actionlistener
instead use anonymous class
or inner classes
some coding :
(retval == true && retval2 == true)
change (retval && retval2)
manage exceptions , never blank catches
} catch (amazonserviceexception ase) { }
Comments
Post a Comment