swing - Java GUI Variables Problems -


i seem have set wrong in action listener create button handler. when tried value of text field nametf null pointer error. tried imitate code calculator, , exit button handler works well. know pressing create button invokes handler statement

inname = nametf.gettext();

gives error message.

the complete text of listener is

private class createbuttonhandler      implements actionlistener     {         public void actionperformed(actionevent e)         {         string inname, intype; //local variables         int inage;         dog arf;         inname = nametf.gettext();         intype = typetf.gettext();         inage = integer.parseint( agetf.gettext() );         //system.out.println("inside createbuttonhandler");         system.out.println(inname);         arf = new dog(inname, intype, inage);         system.out.println(arf.tostring () );      }  } 

and whole program below. help/explanation/suggestion welcome.

import javax.swing.*; // import statement gui components import java.awt.*; //import statement container class import java.awt.event.*;    public class doggui //creation of doggui clas { private jlabel namel, typel, agel, outtputl; private jtextfield nametf, typetf, agetf; private jbutton createb, exitb; createbuttonhandler cbhandler; exitbuttonhandler ebhandler;       public void driver () //creates     {         //create window         jframe doginfo = new jframe ("dog gui");         doginfo.setsize(400,300); //set pixels gui         doginfo.setvisible(true); // set visibility true         doginfo.setdefaultcloseoperation(doginfo.exit_on_close); // when closed jframe disappear  //layout container dogfields = doginfo.getcontentpane(); dogfields.setlayout(new gridlayout(5,2));  // setting labels gui namel = new jlabel ("enter name of dog:  ",                     swingconstants.right); typel = new jlabel ("enter type of dog:  ",                     swingconstants.right); agel = new jlabel ("enter age of dog:   ",                     swingconstants.right); outtputl = new jlabel ("dog information:  ",                     swingconstants.right);  //buttons  jbutton createb, exitb; //creating button creation of dog , button exit createb = new jbutton("create dog"); exitb = new jbutton("exit");  //text fields  jtextfield nametf, typetf, agetf, outtputtf;  nametf = new jtextfield(10); typetf = new jtextfield(15); agetf = new jtextfield(5); outtputtf = new jtextfield(25);  outtputtf.seteditable(false); //this tf display output  //lables , textfields  doginfo.add(namel); doginfo.add(nametf);  doginfo.add(typel); doginfo.add(typetf);  doginfo.add(agel); doginfo.add(agetf);  doginfo.add(outtputl); doginfo.add(outtputtf);  //buttons doginfo.add(createb); doginfo.add(exitb);   //instantiate listeners cbhandler = new createbuttonhandler(); ebhandler = new exitbuttonhandler();  //register listeners createb.addactionlistener(cbhandler); exitb.addactionlistener(ebhandler);  doginfo.setvisible(true); } //run program main, instantiate class, invoke driver() method  public static void main(string[] args) {     doggui d = new doggui();     d.driver(); }  // class handle dog creation  private class createbuttonhandler      implements actionlistener     {         public void actionperformed(actionevent e)         {         string inname, intype; //local variables         int inage;         dog arf;         inname = nametf.gettext();         intype = typetf.gettext();         inage = integer.parseint( agetf.gettext() );         //system.out.println("inside createbuttonhandler");         system.out.println(inname);         arf = new dog(inname, intype, inage);         system.out.println(arf.tostring () );      }  }   private class exitbuttonhandler      implements actionlistener {     public void actionperformed(actionevent e)         {             system.out.println("inside exit button");             system.exit(0);         } // end method actionperformed }   } 

you have local variable in driver() method called nametf hiding field (same other variables).

jtextfield nametf, typetf, agetf, outtputtf;  nametf = new jtextfield(10); 

remove declaration of fields since declared instance fields.

private jtextfield nametf, typetf, agetf; 

(probably not outtputtf, depending on want do)


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -