java - Can't set value on object's variable -


what trying patient id,name,age,disease window 4 text field , search patient details using patient using id.i completed whole task value couldn't save on object's variable though not getting errors.please guys me error.thanks in advance here code:

/* * change license header, choose license headers in project properties. * change template file, choose tools | templates * , open template in editor. */

package hospital.management;  import java.awt.*; import java.awt.event.*;  import javax.swing.*; import sun.security.pkcs11.wrapper.constants;  public class hospitalmanagement {      public  static void main(string[] args) {           new run().setvisible(true);        }   }  class patient{     private              string name,disease ;                   int p_id,p_age;        public                  void get_name(string n){                     name=n;                 }                     void get_id(int id){                     p_id=id;                 }                 void get_disease(string n){                     disease=n;                 }                    void get_age(int age1){                     p_age=age1;                 }              string show_name(){                     return name;             }              int show_id(){                     return p_id;             }              string show_disease(){                     return disease;             }            int show_age(){                     return p_age;             }   }  class run extends jframe{      jframe frame=new jframe();     patient[] p1 = new patient[5];      patient patient = new patient();                         int cout=0;   public run(){   frame.settitle("patient window"); frame.setsize(700,300); frame.setlocation(100,250);  frame.getcontentpane().setlayout(new flowlayout(flowlayout.center,15,15));   final jtextfield jid = new jtextfield(20);   final jtextfield jage = new jtextfield(20);   final jtextfield jname = new jtextfield(20);    final jtextfield jdisease = new jtextfield(20);  final jtextfield jsid = new jtextfield(20);    final jtextarea textarea= new jtextarea();     textarea.seteditable(false);       jlabel lbl_name=new jlabel("name:");     jlabel lbl_id=new jlabel("id: ");     jlabel lbl_age=new jlabel("age: ");     jlabel lbl_disease=new jlabel("disease: ");     jlabel lbl_sid=new jlabel("search id: ");       jbutton submit = new jbutton("submit");     jbutton search = new jbutton("search");        submit.addactionlistener(new actionlistener() {             @override          public void actionperformed(actionevent ev) {               int   id,age;      id = integer.parseint(jid.gettext());            patient.get_id(id); string name= jname.gettext();             patient.get_name(name);         age=integer.parseint(jage.gettext());               patient.get_age(age); string disease=jdisease.gettext();                 patient.get_disease(disease);     }   });       p1[cout]=patient;     cout++;     //a.c++;      search.addactionlistener(new actionlistener() {             @override         public void actionperformed(actionevent ev) {              int  srch_id  = integer.parseint(jsid.gettext());       for(int i=0 ;i<2 ;i++){       if(p1[i].show_id()==srch_id){             textarea.append("id: " +p1[i].show_id()+"    name: "+p1[i].show_name()+"    age: "+p1[i].show_age()+"    disease: "+p1[i].show_disease()+constants.newline);                }   }   }  });   //textarea.append("id: " +p1[cout].show_id()+"    name: "+p1[cout].show_name()+"    age: "+p1[cout].show_age()+"    disease: "+p1[cout].show_disease());  frame.add(lbl_id);     frame.add(jid); frame.add(lbl_name); frame.add(jname); frame.add(lbl_age); frame.add(jage);  frame.add(lbl_disease); frame.add(jdisease);  frame.add(submit);  frame.add(lbl_sid); frame.add(jsid); frame.add(search); frame.add(textarea); frame.pack(); frame.setvisible(true); setdefaultcloseoperation(exit_on_close);  setlocationrelativeto(null);            }  } 

1) bug here:

patient[] p1 = new patient[5];    

you create array null values.

here:

submit.addactionlistener(new actionlistener() {  .... p1[cout].get_id(id); 

you try call get_id array element, null.

i recommend following: 1) in actionperformed method

patient patient = new patient() // classes should start uppercase letter  p1[cout] = patient; 

2) in example better substitute array list

3) seems using getters instead of setters ;)

4) recommend refactor code snippet


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -