ArrayList problem - getting 'cannot resolve symbol' errors when trying to access ArrayList

Posted on 2004-11-13
Last Modified: 2010-03-31
Hi.  I'm just learning java (I know, you've never heard that line before...)

I am writing a program that builds a deck of cards into an array list.  

The errors I am getting are: cannot resolve symbol
symbol  : method size ()
location: class Deck
Object deckSize = theDeck.size();
                                      ^ cannot resolve symbol
symbol  : method get (int)
location: class Deck

Here is the code I have:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Deck extends Object
  static int DECKSIZE;
  protected int remaining;
  private ArrayList theDeck;

  //Default Constructor
  public Deck()
  /* Default constructor creates a deck of 52 cards - Sets remaining to
  DECKSIZE -1 **/
      theDeck = new ArrayList();
      DECKSIZE = 0;
      char suitl = 'E';
      for(int suitno = 0; suitno < 4; suitno ++)
                   case 0: suitl='C';
                   case 1: suitl='D';
                   case 2: suitl='H';
                   case 3: suitl='S';
        for(int rank = 2; rank < 15; rank ++)
            theDeck.add(DECKSIZE, new Card(suitl,rank));
            }//end rank for loop
      }//end suit for loop
            remaining = DECKSIZE -1;
    }//end Default Constructor
   //Main method
public static void main(java.lang.String [] args)
int i = 0;
Deck theDeck = new Deck();
Object deckSize = theDeck.size();
} //end Class

As far as I can tell, my syntax on the get statements to retrieve info from the ArrayList are correct.  Any help would be greatly appreciated.

Question by:Stilett0
    LVL 30

    Accepted Solution

    You have a class-level data-member with the same name as the local-variable in the main () method.

    >> private ArrayList theDeck;

    This is the class-level member.

    >> Deck theDeck = new Deck();

    This is the local variable in main (). Now since they have the same name 'theDeck', this one would over-ride the one in the class-member. Hence, if you want to access the class-member, you should change their names. Also - do you want to access the array-list for the 'theDeck' object? If so, you should provide an accessor method for it. Something like:

    public ArrayList getDeck ()
      return theDeck ;

    } // end of getDeck ()

    From main (), you can write:

    Deck theDeck = new Deck () ;
    System.out.println ( theDeck.getDeck ().get ( 0 ) ) ; // in this case, you can live with the same variable-name.

    Also - if you want to access the ArrayList directly from main (), you cannot do that since the ArrayList is not a static member whereas the main () method is static. You either need to make the array-list static or use an object to access it, the way I've shown.

    As a matter of fact, since the main () method belongs to the same class, you don't need the accessor method, meaning that you can directly use:

    Deck theDeck = new Deck () ;
    System.out.println ( theDeck.theDeck.get ( 0 ) ) ; // I hope the variable-name should not be an issue

    But its better to keep an accessor if you want to access this list from outside the class.
    LVL 24

    Expert Comment

    You are confusing 2 things:
    theDeck is an ArrayList in one place and a Deck in the main.
    And Deck has no .size()
    LVL 1

    Author Comment


    If I was to differentiate the names, which ones do I change?  I tried to use a separate array name in the Main method to begin with, and it also was not working.  

    Your answer was excellent, and the program will work with what you suggested.  I guess I'm just confused as to where the name differentiation needs to occur.

    LVL 30

    Expert Comment

    Better to change the name of the ArrayList to something else.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now