Maxime Painchaud Maxime Painchaud - 11 months ago 69
Java Question

What is wrong with my constructor in java?

Is there a better way (probably..) to build a class in which i can use set/get method.
Notice thay all the data are stock in a ArrayList.

public class PorterList
public PorterList()
ArrayList<Porter> porList = new ArrayList<>();
public PorterList(ArrayList<Porter> p)
ArrayList<Porter> porList = p;

SimpleDateFormat porterDF = new SimpleDateFormat("HH:mm:ss");
private Porter p = new Porter();
private int _porterNo;
public String getStatus(int porterNo)
_porterNo = porterNo;
p = porList.get(_porterNo);
return p.p_state;

There's something wrong on that second last line p = porList.get(_porterNo);

I want to use something like this in my main:

p_L = PorterList(p)
porter_status = p_L.get(5)

Thank you very much

Answer Source

Yor ArrayList is local variable and it's your problem. It's should be a field.

private ArrayList<Porter> porList;
public PorterList() { 
  porList = new ArrayList<>(); 
public PorterList(ArrayList<Porter> p
  porList = p;