当我运行我的程序时,我总是遇到这个错误“线程“AWT-EventQueue-0”java.lang.NullPointerException中的异常”。我有成员类,我在其中编写我的 setter 和 getter。我需要的所有实体都在那里。现在我在“管理类”中有另一种方法
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)。我试图获取文本字段/组合框中的所有文本并进行设置,以便在我编写 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());
}
}
谢谢你的帮助。