java - What is wrong with my login system? -
here's code copy , paste error i'm getting in console.
package com.bob; import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; public class boblogin extends jframe implements actionlistener { // declare of component variables. private jbutton login; private jtextfield usernamefield; private jpasswordfield passwordfield; private jlabel username; private jlabel password; private jframe loginframe; private string usernametext = "username:"; private int usernamecolumns = 20; private string passwordtext = "password:"; private int passwordcolumns = 20; //declare of constants. private static final int width = 300; private static final int height = 300; //login password , username. private static final string correctusername = "admin"; private static final string correctpassword = "password"; //constructor calls initliaziation method when object created. public boblogin() { initializegui(); } //sets login gui. private void initializegui() { jbutton login = new jbutton("login"); jtextfield usernamefield = new jtextfield(usernamecolumns); jpasswordfield passwordfield = new jpasswordfield(passwordcolumns); jlabel username = new jlabel(usernametext); jlabel password = new jlabel(passwordtext); jframe loginframe = new jframe(); jpanel loginpanel = new jpanel(new flowlayout()); loginpanel.add(username); loginpanel.add(usernamefield); loginpanel.add(password); loginpanel.add(passwordfield); loginpanel.add(login); login.addactionlistener(this); loginframe.add(loginpanel); // loginframe.setsize(width, height); loginframe.pack(); loginframe.setlocationrelativeto(null); loginframe.setresizable(false); loginframe.setdefaultcloseoperation(jframe.exit_on_close); loginframe.setvisible(true); //username.req } public static void main(string[] args) { boblogin boblogin = new boblogin(); } //what happens when "login" button clicked. @override public void actionperformed(actionevent e) { string usernameresponse = usernamefield.gettext(); //string passwordresponse = passwordfield.gettext(); if(usernameresponse.equalsignorecase("admin")){ system.out.println("correct"); }else{ system.exit(0); } } }
this error when hit "login". works fine whenever click login button error in console:
@ com.bob.boblogin.actionperformed(boblogin.java:75) @ 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:2719) @ java.awt.component.dispatchevent(component.java:4687) @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:723) @ java.awt.eventqueue.access$200(eventqueue.java:103) @ java.awt.eventqueue$3.run(eventqueue.java:682) @ java.awt.eventqueue$3.run(eventqueue.java:680) @ 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:696) @ java.awt.eventqueue$4.run(eventqueue.java:694) @ java.security.accesscontroller.doprivileged(native method) @ java.security.protectiondomain$1.dointersectionprivilege(protectiondomain.java:76) @ java.awt.eventqueue.dispatchevent(eventqueue.java:693) @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:242) @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:161) @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:150) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:146) @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:138) @ java.awt.eventdispatchthread.run(eventdispatchthread.java:91)
you using instance variable usernamefield
obtain text when actionevent
triggered. problem is, creating new local usernamefield
variable inside of initializegui
method while it's created instance variable it's not initialized. fix problem, initialize usernamefield
variable outside of initializegui
method. working code:
import java.awt.flowlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpanel; import javax.swing.jpasswordfield; import javax.swing.jtextfield; public class boblogin extends jframe implements actionlistener { // declare of component variables. private jbutton login; //here issue fix! private jtextfield usernamefield = new jtextfield(20); private jpasswordfield passwordfield = new jpasswordfield(20);; private jlabel username; private jlabel password; private jframe loginframe; private string usernametext = "username:"; private int usernamecolumns = 20; private string passwordtext = "password:"; private int passwordcolumns = 20; //declare of constants. private static final int width = 300; private static final int height = 300; //login password , username. private static final string correctusername = "admin"; private static final string correctpassword = "password"; //constructor calls initliaziation method when object created. public boblogin() { initializegui(); } //sets login gui. private void initializegui() { jbutton login = new jbutton("login"); jlabel username = new jlabel(usernametext); jlabel password = new jlabel(passwordtext); jframe loginframe = new jframe(); jpanel loginpanel = new jpanel(new flowlayout()); loginpanel.add(username); loginpanel.add(usernamefield); loginpanel.add(password); loginpanel.add(passwordfield); loginpanel.add(login); login.addactionlistener(this); loginframe.add(loginpanel); // loginframe.setsize(width, height); loginframe.pack(); loginframe.setlocationrelativeto(null); loginframe.setresizable(false); loginframe.setdefaultcloseoperation(jframe.exit_on_close); loginframe.setvisible(true); //username.req } public static void main(string[] args) { boblogin boblogin = new boblogin(); } //what happens when "login" button clicked. @override public void actionperformed(actionevent e) { string usernameresponse = usernamefield.gettext(); //string passwordresponse = passwordfield.gettext(); if(usernameresponse.equalsignorecase("admin")){ system.out.println("correct"); }else{ system.exit(0); } } }
Comments
Post a Comment