Java - Arraylists

Soldato
Joined
24 Apr 2011
Posts
5,455
I created a client object and I want to create an array list that stores the information client objects.

Code location: Client.java
Code:
 ArrayList<Client> ClientInfo = new ArrayList<Client>();

    public ArrayList<Client> getClientInfo() {
        return ClientInfo;
    }


Code Location: Client Handling.java
Code:
c.setClientId(Client.getClientInfo().size());
        
        Client.getClientInfo().add(c);

Error at ClientHandling.java:


ncilHaCfjUY6JeszCnWJ.png



ySzyKvM2qT9mu7pqBCmZ.png


Any ideas?
 
Last edited:
The problem you're having is you're not referencing an instance of the class. You're referencing the class it's self. You can only call static methods like this.

It doesn't sound like you want to make that method static though, (and you can't because it's using a non-static variable).


There's not enough code pasted to see how that class fits into it all. However, you'll probably want something like:
Code:
Client clientInstance = new Client();

c.setClientId(clientInstance.getClientInfo().size());
        
clientInstance.getClientInfo().add(c);

Looking at the that is present though, I'm not sure if you're trying to get what you need out of it?

Is there a reason that a Client object contains a list of other Client objects? It sounds to me like that list should be in another class?
 
Last edited:
Back
Top Bottom