java - Variable's value doesn't change to the new one specified in the actionListener -


so created variable called savedname , set value of new value in action listener method. prints changed value in action listener in action listener. seems once action listener has taken place, variable's value changed "null" (the default value). username_txt text field user enters information. stated, value changed temporarily in action listener method seems , once action listener has taken place, changes default value. have bolded statements savedname.

public class loginwindow extends javax.swing.jframe {      /**      * creates new form loginwindow      */     connection conn = null;     resultset rs = null;     preparedstatement pst = null;     **private string savedname;**     public loginwindow() {         initcomponents();         conn = javaconnect.connecrdb();     } private void jbutton2actionperformed(java.awt.event.actionevent evt) {                                              // todo add handling code here:     string user_sql = "select * users username=? , password=? ";     try{         pst = conn.preparestatement(user_sql);         pst.setstring(1, username_txt.gettext());         savedname = username_txt.gettext();         system.out.println(savedname);         pst.setstring(2, password_txt.gettext());          rs = pst.executequery();         if(rs.next()){             joptionpane.showmessagedialog(null, "wecome!");             rs.close();             pst.close();             this.dispose();              usermainwindow wind = new usermainwindow();             wind.setvisible(true);         }         else{             joptionpane.showmessagedialog(null, "username and/or password not correct");         }     }     catch(exception e){         joptionpane.showmessagedialog(null,e);     }     {         try{         rs.close();          pst.close();         }         catch(exception e){          }      } }                 

you getting null other class because creating new object rather passing value other class. can done putting constructor inside usermainwindow class , accepting string name. if have constructor accept string parameter , create variable string , set it..

example code : (note usermainwindow class)

private string savedname; public usermainwindow(string savedname){   this.savedname = savedname; } 

now in class creating object, pass string this: (and class creating usermainwindow class)

usermainwindow wind = new usermainwindow(savedname); 

now should have savedname saved inside other frame (user main window) while other frame discarded.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -