Click here to Skip to main content
15,867,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some big long code from a group project I'm working on. We all add our own separate classes to an "Army" class that consists of a GUI showing each of our different classes (like "Archer, Ninja, Catapult, etc.).

Anyway, I want to create an ArrayList that records each of the objects instantiated by the Army. However, as soon as I try to add anything to the ArrayList, it gives me the error <identifier> expected.

These two lines compile fine when I add them:
Java
   ArrayList<object>armyList = new ArrayList<object>();
   Warrior warrior = new Warrior();
But as soon as I add the line <pre lang="java"> armyList.add(warrior);
It gives me the aforementioned error.

What's going on??

Here's the full code of the Army class (I didn't want to clutter up this question space with the full thing).

What I have tried:

Researching the Internet extensively. Making sure I am declaring and adding to ArrayLists correctly. Isolating the problem into a separate file.
Posted
Updated 22-Oct-16 12:43pm

1 solution

Turns out I couldn't call methods (like the .add() method) from directly within the class, not in a method. Problem solved :/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900