0

I have 2 forms , one form for registration and other form for searching records,deleting,etc. In the registration form i put the registration details in to an object and then store it in a hashmap. In the second form i access the hashmap to delete ,search records according to the id saved(Key).

My problem is when i save a record and access it from the second form (access the hashmap), i get null pointer exception. (I get null pointer exception , even if i have a record saved to the searched key)

Here is what i have done:

Registration form code :

 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();
       }

    }                                           

Here is the Maps Class(All the maps are in this class) :

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);
    }




}

Here is the code which i use in the second form to search a record :-

        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());

    }     

What seems to be the problem here ?

Thank you for your time.

Troller
  • 1,108
  • 8
  • 29
  • 47
  • I don't get why you instantiate `storage` twice? – varnie Aug 31 '13 at 11:45
  • the second instantiate is in another form. (Second form ) – Troller Aug 31 '13 at 11:46
  • 1
    this is wrong. if I understood your problem correctly, you want your object (map) being accessible globally through all your forms. So, there should be a single instance of it. Regarding your problem: map returns `null` if there's no such item having key `memberID`, which you are trying to stringify. Invoking `toString()` on null causes that exception. – varnie Aug 31 '13 at 11:49
  • Any idea on how to have just one single instance for all forms to access the map? Yes, if there is no key having the given memberID i get null pointer(didn't bother about that part still) but the problem is even if there is key with the given memberID i still get the null pointer exception. – Troller Aug 31 '13 at 11:53
  • as I said, you're operating here on the second, unique, map object which doesn't contain that entry. (the entry is still in the map object created in the first form). – varnie Aug 31 '13 at 12:01

1 Answers1

2

You are creating Maps storage=new Maps(); in both the forms. Maps has

Map<String,Members> memberMap=new HashMap();

as non static field. So whenever you create a new Maps() it will create a new memberMap inside.

An alternative would be have memberMap as static (shared variable) and provide static access methods to them

public class Maps {

  private static Map<String,Members> memberMap=new HashMap<>();

  public static Map<String, Members> getMemberMap() {
        return memberMap;
  }


  public static void setMemberMap(String key,Members object) {
      memberMap.put(key,object);
  }

}

In this way, you can add items in one form

Maps.setMemberMap(memberid, newMember);

And get the same value from other form

Maps.getMemberMap().get(memberID);

Remember-

private static objects are not garbage collected, unless the holder class gets GCd. So unwanted items should be manually removed from the map, time to time.

Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64