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

Posted on 2004-11-13
Medium Priority
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:

Deck.java:85: cannot resolve symbol
symbol  : method size ()
location: class Deck
Object deckSize = theDeck.size();
Deck.java:86: 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
  • 2
LVL 30

Accepted Solution

Mayank S earned 1200 total points
ID: 12577439
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

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

Author Comment

ID: 12577453

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

by:Mayank S
ID: 12592224
Better to change the name of the ArrayList to something else.

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month13 days, 22 hours left to enroll

807 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