Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: trouble with ArrayList

  1. #1
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default trouble with ArrayList

    I hope my code (below) is self-explanatory. The compiler doesn't like it, though. It says:
    "cannot find symbol
    symbol: method getX()
    location: class java.lang.Object"

    I don't get it. getX() is definitely a method of the class Point2D.Double, so what's the problem?

    Please help. Thanks.

    Java Code:
             ArrayList testList = new ArrayList<Point2D.Double>(2);
                
             testList.add(new Point2D.Double(3,5));
             testList.add(new Point2D.Double(4,6));
             messageBox4.setText( // messageBox4 is a JTextArea
               "testList.size() is: " + testList.size() + "\n" +
               "firstPoint.x: " + testList.get(0).getX()
                     );

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: trouble with ArrayList

    While you have created a generic ArrayList<Point2D> object (no need for the Double part), you don't have a generic ArrayList<Point2D> variable, and so the compiler won't know that it holds Point2D objects. Make the variable generic too.
    rippon likes this.

  3. #3
    rippon is offline Member
    Join Date
    Nov 2010
    Posts
    38
    Rep Power
    0

    Default Re: trouble with ArrayList

    (Feel quite stupid now - the answer was pretty obvious, but just couldn't see it.)

    Thanks. Your answers are always remarkably quick, thorough and clear.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: trouble with ArrayList

    You're welcome, and glad it helped!

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  2. Replies: 7
    Last Post: 11-22-2010, 01:26 PM
  3. arraylist trouble
    By Reiyn in forum New To Java
    Replies: 9
    Last Post: 05-27-2009, 11:03 PM
  4. Trouble with ArrayList
    By John D. in forum New To Java
    Replies: 4
    Last Post: 03-18-2009, 07:43 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •