简体   繁体   中英

Accessing ArrayList from a different class

I struggle to find out how do I access the ArrayList from another class.I read hundreds of similar topics but can't figure it out what's wrong exactly from what I am trying to do. I have below the setLoginChoice which should read the ArrayList from login class.I am trying also to make the ArrayList dynamically so can update with the new entries from the user. I am wondering why when I am reading the ArrayList is giving me null value.

EDIT

Maybe I am dumb but from I understand, the ArrayList I created is based on the customer method right?(with the same number of arguments). So when I am trying to read the "apostcode" ( one element from each list from the array) I am actually reading the "apostcode" from the method "Customer" which gives me a null value. What is the right way to do it :)?

  public class Customer {

   private static String customerId;
   private static String firstName, lastName;
   private static String address, dateOfBirth, telephoneNumber, cprNumber;
   private static String userName, password, logInCredentials;
   private static  int postCode;
   String auserName;
   String apassword;
   String apostCode;
   String adateOfBirth ;
   String afirstName ;
   String alastName ;
   String aaddress ;



        public Customer (String afirstName,String aaddress ,String alastName, int apostCode, String acprNumber, String atelephoneNumber, String adateOfBirth , String auserName, String apassword)
    {
        adateOfBirth = dateOfBirth;
        afirstName = firstName;
        alastName = lastName;
        apostCode = postCode;
        aaddress = address;
        acprNumber = cprNumber; 
        atelephoneNumber = telephoneNumber;
        apassword = password;
        auserName = userName;


    }

   public static boolean setLoginChoice (int choiceForLogIn) {  
    ArrayList <Customer> myUsers = (new Login()).login();

    Scanner input = new Scanner(System.in);
        System.out.println("Enter your username: ");
        inputUsername = input.nextLine();
        System.out.println("Enter your password: ");
        inputPassword = input.nextLine();
        System.out.println("before");

        for(int i = 0; i < myUsers.size(); i++){
            System.out.println("loopfor");
            System.out.println(myUsers.get(i).apostCode);


            /*if(myUsers.get(i).auserName.compareTo(getInputuserName()) == 0 && myUsers.get(i).apassword.compareTo(getInputPassword()) == 0)
            {
            System.out.println("Welcome");
            return true;

            }else {
                System.out.println("insideloop");
            }*/

        }
        System.out.println("after");
        return false;
    }




     public class Login{

       ArrayList<Customer> myUsers;

       public ArrayList<Customer> login() {

    // TODO Auto-generated method stub
    ArrayList<Customer> myUsers = new ArrayList<Customer>();
    Customer d = new Customer(null, null, null, 0, null, null, null, null, null);
    myUsers = new ArrayList<Customer>();

    myUsers.add(new Customer("User1", "bane", "User1nick", 7444, "111116-4421", "55555555", "11/05/1993", "User3", "Joe"));
    myUsers.add(new Customer("User2", "bane", "User2nick", 7444, "111116-4021", "55555555", "11/05/1993", "User3", "Stefan2"));
    myUsers.add(new Customer("User3", "bane", "User3nick", 7444, "111116-4021", "55555555", "11/05/1993", "User3", "Adi2"));
    myUsers.add(new Customer(d.getfirstName(), d.getAddress(), d.getlastName(), d.getPostCode(), d.getCprNumber(), d.getTelephoneNumber(), d.getdateOfBirth(), d.getUsername(), d.getPassword()));;

    return myUsers; 
 }


}
private static String customerId;
 private static String firstName, lastName;
 private static String address, dateOfBirth, telephoneNumber, cprNumber;

Those variables are always null.

When you debug your code you can see that you overwrite your values to null.

    adateOfBirth = dateOfBirth;
    afirstName = firstName;
    alastName = lastName;
    apostCode = postCode;
    aaddress = address;
    acprNumber = cprNumber; 
    atelephoneNumber = telephoneNumber;
    apassword = password;
    auserName = userName;

So all the elements are null in array. Because when this line is executed.

myUsers.add(new Customer("User1", "bane", "User1nick", 7444, "111116-4421", 
"55555555", "11/05/1993", "User3", "Joe"));

It call Customer function and all parameters are right, but when it return all the values is null as already said. And the code is like

 myUsers.add(null, null, etc...);

You have issues with your constructor. You should initialize object data with data passed in constructor, like this:

public Customer (String afirstName,String aaddress ,String alastName, int apostCode, String acprNumber, String atelephoneNumber, String adateOfBirth , String auserName, String apassword){
    this.adateOfBirth = adateOfBirth ; // adateOfBirth instead of dateOfBirth
    this.afirstName = afirstName ; // ...
    this.alastName = alastName ;
    this.apostCode = apostCode ;
    this.aaddress = aaddress ;
    this.acprNumber = acprNumber ; 
    this.atelephoneNumber = atelephoneNumber ;
    this.apassword = apassword ;
    this.auserName = auserName ;
}

Not sure what did you intended with static fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM