1

I have a Staff class which contains a list of roles for that particular staff member. I also have staffList which holds the staff objects.

How do I get an element from the rolesList for that specific instance of the class?

I have tried:

staffList.get(0).getRolesList().get(0)

Which should be the first element of the rolesList from the first element of the staffList, but it just ruturns null.

I also tried:

rolesList.get(staffList.get(0))
rolesList.getIndexOf(staffList.get(0).getRolesList.get(0)

All return null

If I just get the value direct from the rolesList using get index it will display no problem.

I think it is getting a version of the rolesList but not the one that is in that particular Staff object

I have created a new Staff Member and a role in the roleList then used the list when I construct the staff object, so using the getRolesList method of that object and then get the index it should return the value from within the roleList but it isn't:

private List<Staff> staffList = new ArrayList();
ArrayList<Role> roleList = new ArrayList();
roleList.add(Role.DRIVER);
testDriver = new Staff("Mike", "Joy", roleList);
testStaffList.add(testDriver); 

GetRolesList() Code

public List<Role> getRoleList() {
        return roleList;
    }

I basically want to get the stored Role from the objects rolelist within the testdriver object.

testDriver --> roleList --> Role.DRIVER (or whatever the Role happens to be)

SLePort
  • 15,211
  • 3
  • 34
  • 44
  • As i can see on the last pieces of code, `staffList` is not related with the other objects at any point. Are you missing to paste something else? – Lucio Aug 14 '16 at 01:38
  • Could you paste code from the `getRolesList()` function? It is unclear what the problem is without further examples of the code you have used – dunck Aug 14 '16 at 01:39

2 Answers2

0

(a) We do not see the line of code adding testDriver to the staffList.

staffList.add( testDriver );

(b) You are missing parens on your call to getRolesList.

(c) You need to do some basic debugging. In the debugger or in extra code, look at:

  • size of staffList
  • Staff s = staffList.get(0)
  • List roles = s.getRolesList()
  • size of roles

Bonus tip… Apparently you are using an enum for Role. If so, you should consider using an EnumSet rather than a List for less memory usage and faster execution. EnumSet is a specialized implementation of Set. More discussion on another Question. Not part of your problem, just a tip.

Set<Role> roles = EnumSet.of( Role.DRIVER , Role.MECHANIC );
Staff testDriver = new Staff( "Mike" , "Joy" , roles );
Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Could you give your full source.. That's not clear. I think u wanna add your staff object into staff list.. If it is

Stafflist.add(new Staff("a","b",role list));

Add all you want Then try to get ..