在setter和getter中获取NULL - JAVA

时间:2012-12-19 02:40:18

标签: java

我总是在运行我的程序时遇到此错误“线程中的异常”AWT-EventQueue-0“java.lang.NullPointerException”。我有会员班,我编码我的二传手和吸气剂。我需要的所有实体都在那里。现在我在“Admin Class”中有另一种方法

 public class Admin {
     private ArrayList<Member> members;
     private String AccessName;
     private String username;
     private String password;

     public Admin(){
        this.AccessName="Ms. April Santos";
        this.username = "estella";
        this.password = "teller";
        this.members = new ArrayList<Member>();
    }

    public ArrayList<Member> getMembers() {
        return members;
    }

    public void setMembers(ArrayList<Member> members) {
        this.members = members;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAccessName() {
        return AccessName;
    }

    public void setAccessName(String AccessName) {
        this.AccessName=AccessName;;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Admin other = (Admin) obj;
        if ((this.username == null) ? (other.username != null) : !this.username.equals(other.username)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 97 * hash + (this.username != null ? this.username.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "Admin{" + "members=" + members + ", AccessName;=" + AccessName + ", username=" + username + ", password=" + password + '}';
    }

       public Member searchMember(Member member){
        return this.searchMember(member.getMemberId());
    }

         public Member searchMember(String memberId){
          for(Member member : this.members){
            if(member.getMemberId().equals(memberId)){
                return member;
            }
          }
          return null;
    }

    public Member addMember(Member member){
        if (this.searchMember(member) == null){
            this.members.add(member);
            return member;
        }
        return null;
    }

    public Member addMember(String memberId, String firstName,String middleName, String lastName){
        if (this.searchMember(memberId) == null){
            Member member = new Member(memberId, firstName,middleName, lastName);
            this.members.add(member);
            return member;
        }
        return null;
    }

    public boolean validateLogin(String username, String password){
        boolean valid = false;
        valid = this.username.equals(username) && this.password.equals(password);
        return valid;
    }

   }

现在我在AddMemberView(GUI)中调用public addMember(String memberId,String firstName,String middleName,String lastName)。我试图获取textfield / combobox中的所有文本并将其设置为临时保存并准备好在我编码DAO时保存在数据库中。这是我的AddMemberView btnSave动作外观:

 import java.sql.SQLException;
 import java.text.*;
 import java.util.*;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.JOptionPane;
 import rtu.tomis.bom.Admin;
 import rtu.tomis.bom.Member;
 import rtu.tomis.dao.MemberDao;

 public class AddMemberView extends javax.swing.JPanel {

 private MainFrameView mainFrameView;
 private Member member;
  private Admin admin;

  AddMemberView(Member memb, Admin admin){

    this.member= memb;
    this.admin= admin;

}

public AddMemberView() {
    initComponents();
}

  public AddMemberView(MainFrameView mf) {
    this.mainFrameView = mf;
    initComponents();
}

    public AddMemberView(Member m) {
    this.member = m;
    initComponents();
}

  private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {                                        
    String memberid = (this.txtMembersID.getText());
    String fname= (this.txtFirstName.getText());
    String mname=(this.txtMiddleName.getText());
    String lname=(this.txtLastName.getText());
    if(this.admin.addMember(memberid, fname,mname, lname) != null)//im having null here
    {
       try{
            member = this.admin.searchMember(memberid);// and if i remove that 1st line of null. the next null pointer is here
            String month = (String)cmbMonth.getSelectedItem();
            Integer day = (Integer) cmbDay.getSelectedItem();
            Integer year = (Integer)cmbYear.getSelectedItem();
            String bday = month + " " + day + ", " + year;
            DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US);
            Date dateBday = df.parse(bday);
            String Block = this.txtBlkLotNo.getText();
            String Street = (String)this.cmbStreet.getSelectedItem();
            String Brgy=(String)this.cmbBarangay.getSelectedItem();
            String City= (String)this.cmbCity.getSelectedItem();
            String Address = Block + " " + "" + Street + " ," +  Brgy + " ," + City;
            String Full = fname + " " + mname + " " + lname;
            int age= Integer.parseInt(this.txtAge.getText());
            int contact = Integer.parseInt(this.txtContact.getText());
            String region =(String) this.cmbRegion.getSelectedItem();

            this.member.setFirstName(fname);// then if i remove the two my next null line is here and those who have this.**member**.setblabla.
            this.member.setMiddleName(mname);
            this.member.setLastName(lname);
            this.member.setFullname(Full);
            this.member.setLotNo(Block);
            this.member.setBarangay(Brgy);
            this.member.setCity(City);
            this.member.setFulladdress(Address);
            this.member.setBirthday(dateBday);
            this.member.setAge(age);
            this.member.setEmailAddress(this.txtEadd.getText());
            this.member.setVotersId(this.txtVotersID.getText());
            this.member.setContactNo(contact);
            this.member.setRegion(region);
            this.member.setMemberId(memberid);
           if(this.rbtnFemale != null){
               this.member.setGender(this.rbtnFemale.getLabel());
           }
               else{
               this.member.setGender(this.rbtnFemale.getLabel());
           }

       }catch (ParseException ex) {
            System.out.println("Exception: "+ ex.getMessage());
        }
        JOptionPane.showMessageDialog(this, "Info saved!");
        JOptionPane.showMessageDialog(this,"Name" + this.member.getFirstName());

    }

}                  

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

我认为你的“admin”变量为null。你打电话给你的构造函数?如果是,参数是否正确?

只需编写“私人管理员管理员”,不要将管理员变量设为管理员对象。尝试初始化它或正确调用构造函数。