Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How do i access methods from an arraylist?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Apr 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do i access methods from an arraylist?

    Ok i have the following client class

    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?
    Last edited by Freaky Chris; April 7th, 2010 at 08:52 AM. Reason: Add code tags

  4. #4
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM
  3. How can i store ArrayList objects in Access database
    By frankycool in forum JDBC & Databases
    Replies: 0
    Last Post: November 4th, 2009, 12:44 AM
  4. Arraylist or Arraylist Object?
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: September 11th, 2009, 03:08 AM
  5. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 02:11 PM