文本字段中数字的Arraylist

时间:2012-12-14 02:49:03

标签: java swing nullpointerexception

这是我在尝试执行程序时遇到的错误。老实说,我真的不知道从哪里开始我的代码。 我有一个新旧按钮名称。 我希望textfieldMemberID显示yearToday和一组加入组织的成员。例如,如果我点击new,textfieldMemberId值将自动显示“2012-000001”,如果ic再次点击new,则会显示“2012-000002”。 “          我有一个会员班:

     import java.text.DecimalFormat;
     import java.text.NumberFormat;
     import java.util.ArrayList;
     import java.util.Calendar;
     import java.util.Date;


      public class Member {
      private ArrayList accounts;
      private String MemberId;
      private String VotersId;
private String FirstName;
private String MiddleName;
private String LastName;
private String LotNo;
private String Street;
private String Barangay;
private String City;
private String Region;
private int Age;
private String Gender;
private Date Birthday;
private String ContactNo;
private String EmailAddress;
private int size=0;
private int yearToday = Calendar.getInstance().get(Calendar.YEAR);

public Member(String MemberId) {
   NumberFormat formatter = new DecimalFormat("000000");
    size= this.accounts.size()+1;
    this.setMemberId(""+yearToday + "-" + (formatter.format(size)));  
} //im trying to have the solution here

public Member(String MemberId,String FirstName,String MiddleName,String LastName){
    this.setMemberId(MemberId);
    this.setFirstName(FirstName);
    this.setMiddleName(MiddleName);
    this.setLastName(LastName);
}


public int getAge() {
    return Age;
}

public void setAge(int Age) {
    this.Age = Age;
}

public String getBarangay() {
    return Barangay;
}

public void setBarangay(String Barangay) {
    this.Barangay = Barangay;
}

public Date getBirthday() {
    return Birthday;
}

public void setBirthday(Date Birthday) {
    this.Birthday = Birthday;
}

public String getCity() {
    return City;
}

public void setCity(String City) {
    this.City = City;
}

public String getContactNo() {
    return ContactNo;
}

public void setContactNo(String ContactNo) {
    this.ContactNo = ContactNo;
}

public String getEmailAddress() {
    return EmailAddress;
}

public void setEmailAddress(String EmailAddress) {
    this.EmailAddress = EmailAddress;
}

public String getFirstName() {
    return FirstName;
}

public void setFirstName(String FirstName) {
    this.FirstName = FirstName;
}

public String getGender() {
    return Gender;
}

public void setGender(String Gender) {
    this.Gender = Gender;
}

public String getLastName() {
    return LastName;
}

public void setLastName(String LastName) {
    this.LastName = LastName;
}

public String getLotNo() {
    return LotNo;
}

public void setLotNo(String LotNo) {
    this.LotNo = LotNo;
}

public String getMemberId() {
    return MemberId;
}

public void setMemberId(String MemberId) {
    this.MemberId = MemberId;
}

public String getMiddleName() {
    return MiddleName;
}

public void setMiddleName(String MiddleName) {
    this.MiddleName = MiddleName;
}

public String getRegion() {
    return Region;
}

public void setRegion(String Region) {
    this.Region = Region;
}

public String getStreet() {
    return Street;
}

public void setStreet(String Street) {
    this.Street = Street;
}

public String getVotersId() {
    return VotersId;
}

public void setVotersId(String VotersId) {
    this.VotersId = VotersId;
}

public ArrayList getAccounts() {
    return accounts;
}

public void setAccounts(ArrayList accounts) {
    this.accounts = accounts;
}


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

@Override
public int hashCode() {
    int hash = 5;
    hash = 89 * hash + (this.MemberId != null ? this.MemberId.hashCode() : 0);
    return hash;
}

@Override
public String toString() {
    return "Member{" + "MemberId=" + MemberId + ", VotersId=" + VotersId + ", FirstName=" + FirstName + ", MiddleName=" + MiddleName + ", LastName=" + LastName + ", LotNo=" + LotNo + ", Street=" + Street + ", Barangay=" + Barangay + ", City=" + City + ", Region=" + Region + ", Age=" + Age + ", Gender=" + Gender + ", Birthday=" + Birthday + ", ContactNo=" + ContactNo + ", EmailAddress=" + EmailAddress + '}';
}
}

我试图通过Button“New”中的代码调用getmemberId

String last="";
last = member.getMemberId();
this.txtMembersID.setText((last));

但我收到了这个错误。     线程中的异常“AWT-EventQueue-0”java.lang.NullPointerException 请帮助。谢谢你。

1 个答案:

答案 0 :(得分:3)

问题出现在第一个构造函数的这一行:

size= this.accounts.size()+1;

因为accounts尚未初始化。

在此之前添加此行:

accounts = new ArrayList();

应该解决你的问题。


此外,为了将来参考,您可以在此处提供的最有用的信息之一是导致异常的行数 - 使事情变得更容易解决!