我有 2 个表格,一个表格用于注册,另一个表格用于搜索记录、删除等。在注册表单中,我将注册详细信息放入对象中,然后将其存储在哈希图中。在第二种形式中,我访问要删除的哈希图,根据保存的 id(键)搜索记录。
我的问题是当我保存记录并从第二种形式访问它(访问哈希图)时,我得到空指针异常。(我得到空指针异常,即使我有一条记录保存到搜索的键)
这是我所做的:
报名表代码:
Maps storage=new Maps();
private void btnRegisterActionPerformed(java.awt.event.ActionEvent evt) {
try{
String firstName=txtFirstName.getText();
String initials=txtInitials.getText();
String birthDay=cmbBirthDay.getSelectedItem().toString();
String birthMonth=cmbBirthMonth.getSelectedItem().toString();
String birthYear=txtBirthYear.getText();
String Gender=cmbGender.getSelectedItem().toString();
String address=txtAddress.getText();
String tele=txtTele.getText();
String membershipType="";
String paymentType="";
String memberid=txtMemberId.getText();
if(rbPlatinum.isSelected());{
membershipType=rbPlatinum.getText();
}
if(rbGold.isSelected());
{
membershipType=rbGold.getText();
}
if(rbSilver.isSelected());
{
membershipType=rbSilver.getText();
}
if(rbAnually.isSelected());{
paymentType=rbAnually.getText();
}
if(rbSemi.isSelected());{
paymentType=rbSemi.getText();
}
if(rbMonthly.isSelected());{
paymentType=rbMonthly.getText();
}
Members newMember=new Members(firstName,initials,birthDay,birthMonth,birthYear,Gender,memberid,address,tele,membershipType,paymentType);
storage.setMemberMap(memberid, newMember);
JOptionPane.showMessageDialog(frame,"Successfully Registered");
}catch(Exception e){
JOptionPane.showMessageDialog(frame,"ERROR!");
e.getStackTrace();
}
}
这是地图类(所有地图都在这个类中):
public class Maps {
public Maps(){}
Map<String,Members> memberMap=new HashMap();
//returns the memberMap
public Map<String, Members> getMemberMap() {
return memberMap;
}
//add items to map
public void setMemberMap(String key,Members object) {
memberMap.put(key,object);
}
}
这是我在第二种形式中用于搜索记录的代码:-
Maps storage=new Maps();
private void btnSearchMemberActionPerformed(java.awt.event.ActionEvent evt) {
String memberID=txtSearchId.getText();
//get the record and display
txtArea.setText(storage.getMemberMap().get(memberID).toString());
}
这里似乎有什么问题?
感谢您的时间。