How do i access methods from an arraylist?
At the moment i have an arraylist as so:
ArrayList<Client> clients = new ArrayList<Client>();
and I've filled this arraylist with the details of the clients themselves, including things like their name, address, salary, etc.
I can't seem to access the methods of the Client class from the ArrayList, all I end up with is an error when i try to do so. For example, I tried to access the address details of the first client in the arraylist as so:
clients.get(0).getAddress();
but this just gave me an error. Can someone please tell me what im doing wrong?
Re: How do i access methods from an arraylist?
Could you please post your code and then the exact error message you are getting please. As it stands you appear to be doing nothing wrong, but your code must have a flaw somewhere :)
Sorry for the slow reply, was at lunch.
Chris
Re: How do i access methods from an arraylist?
Ok i have the following client class
Code :
import java.util.*;
public class Client
{
private String name;
private String address;
private int phone;
private String service;
private static int jobID = 1;
private int jobID1;
public Client(String name, String address, int phone, String service)
{
this.name = name;
this.address = address;
this.phone = phone;
this.service = service;
this.jobID1 = jobID;
jobID++;
}
public String getName()
{
return name;
}
public String getAddress()
{
return address;
}
public int getPhone()
{
return phone;
}
public int getJobID()
{
return jobID;
}
public void setName(String name)
{
this.name = name;
}
public void setAddress(String address)
{
this.address = address;
}
public void setService(String service)
{
this.service = service;
}
public String toString()
{
return "\n\n\tCLIENT DETAILS\n\t--------------\n" + "\n\tClient Name: " + name + "\n\tClient Address: " + address + "\n\tClient Phone No.: " + phone + "\n\tBooked Service: " + service + "\n\tJob ID Number: " + jobID1 + "\n";
}
}
Now i want to be able to call the getAddress() method from the arraylist i mentioned in my earlier post. How do i do this?
Re: How do i access methods from an arraylist?
Can you post the code ytou are using to call the method from the array list, the whole Driver class you are using