Results 1 to 18 of 18
  1. #1
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Error I don't understand

    Line 30 of this code gives me an error:

    Java Code:
    package com.kingsofwar.main;
    
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    import java.awt.event.MouseListener;
    import java.util.ArrayList;
    
    public class MouseControls implements MouseMotionListener, MouseListener {
    
    	public ArrayList<Card> cards = new ArrayList<Card>();
    	
    	@Override
    	public void mouseDragged(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		// TODO Auto-generated method stub
    	}
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		System.out.println("hi");
    		ArrayList cards = getCards();
    		for (int i = 0; i < cards.size(); i++) {
    				if (new Rectangle(cards.get(i).x + 18, cards.get(i).y, 27, 30).contains(e.getX(), e.getY())) {
    					System.out.println(i);
    					//ArrayList cards = getCards();
    					//for (int k = 0; k < cards.size(); k++) {
    						//Card c = (Card) cards.get(i);
    						System.out.println("msg");
    					break;
    				}
    		}
    	}
    
    	@Override
    	public void mousePressed(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    
    	@Override
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    
    	}
    	
    	public ArrayList getCards() {
    		return cards;
    	}
    
    }
    Here's the Card class:

    Java Code:
    package com.kingsofwar.main;
    
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.util.ArrayList;
    import java.util.Random;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    public class Card {
    	
    	public int x;
    	public int y;
    	private int cardID;
    	private int atk, hp, rng, mvt;
    	
    	public Card(int x, int y, int cardID, boolean cardAlive){
    		this.x = x;
    		this.y = y;
    		//this.addMouseListener(this);
    		//this.addMouseMotionListener(this);
    	}
    
    	public int getX(){
    		return x;
    	}
    	
    	public int getY(){
    		return y;
    	}
    	
    	public int getCardID(){
    		return cardID;
    	}
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,710
    Rep Power
    5

    Default Re: Error I don't understand

    Okay, well when you have the time post the actual error you are getting and then we'll see what can be done about it.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Error I don't understand

    "x cannot be resolved or is not a field."

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Error I don't understand

    You local declaration of ArrayList
    Java Code:
    ArrayList cards = getCards();
    Is hiding your instance declaration. The reason that you got the error is because ArrayList is a generic type but your
    local declaration declared it as a raw type, so it can't determine x for any of its contents.

    Solution:

    Change the above to
    Java Code:
    cards = getCards();
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Error I don't understand

    Thanks Jim. I don't fully understand generic / raw types so I'll look into it.
    Anyway, it fixed my problem.
    Sorry can't rep you again. :(

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error I don't understand

    Quote Originally Posted by supremegrandruler View Post
    Thanks Jim. I don't fully understand generic / raw types so I'll look into it.
    Anyway, it fixed my problem.
    Sorry can't rep you again. :(
    Why not? I did it on behalf of you (I just clicked that little star icon) and nothing exploded nor did I see daemons fly out of my nose ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Error I don't understand

    Quote Originally Posted by JosAH View Post
    Why not? I did it on behalf of you (I just clicked that little star icon) and nothing exploded nor did I see daemons fly out of my nose ...

    kind regards,

    Jos
    Why not? Because:

    "You must spread some Reputation around before giving it to jim829 again."

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error I don't understand

    Quote Originally Posted by supremegrandruler View Post
    Why not? Because:

    "You must spread some Reputation around before giving it to jim829 again."
    Really? I didn't know that; silly forum software ... (I know, I know, they want to prevent A repping B, B repping A, ad nauseam ...)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Error I don't understand

    Quote Originally Posted by jim829 View Post
    You local declaration of ArrayList
    Java Code:
    ArrayList cards = getCards();
    Is hiding your instance declaration. The reason that you got the error is because ArrayList is a generic type but your
    local declaration declared it as a raw type, so it can't determine x for any of its contents.

    Solution:

    Change the above to
    Java Code:
    cards = getCards();
    Regards,
    Jim
    Though that will work, it is a teensy bit pointless.
    May as well use cards directly, rather than assign itself to itself.

    In addition, since the getCards() method is defined as:
    Java Code:
        public ArrayList getCards() {
            return cards;
        }
    it will still generate a warning from the compiler as the return type is raw. Code will work, admittedly.

    ETA: Oh, and Mr supremegrandruler (nice name, you have clearly read ego-less programming), you can always give me the rep...at least that way I'll be back at evens with Jos and the balance of the universe will be restored.
    Last edited by Tolls; 05-12-2014 at 06:15 PM. Reason: Shameless self promotion.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Error I don't understand

    Yeah. Now that you mention it, perhaps using a local value and making it generic:

    ArrayList<Card> cards = getCards();

    would be better. I believe that a using a class' own getters and setters internally is a good
    habit to get into.

    And I did not catch the bad return type on the getCards method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error I don't understand

    Quote Originally Posted by Tolls View Post
    you can always give me the rep...at least that way I'll be back at evens with Jos and the balance of the universe will be restored.
    But only after you'e rep'ed me or you can rep Jim, but Jim has to rep me then; only cycles restore the balance in our little holy matrix. Closures over the rows; that's the only solution ...

    kindest regards,

    Jos (<--- perfectly balanced)
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Error I don't understand

    Of course, considering both your rep points vs mine, I would benefit the most. Also, I don't think folks even understand the rep system (I'm not certain I understand it myself). They rep with no rep power. But its a nice gesture anyway.

    Regards,
    Jim
    Last edited by jim829; 05-12-2014 at 09:51 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: Error I don't understand

    Quote Originally Posted by jim829 View Post
    Of course, considering both your rep points vs mine, I would benefit the most. Also, I don't think folks even understand the rep system (I'm not certain I understand it myself). They rep with no rep power. But its a nice gesture anyway.

    Regards,
    Jim
    It's utterly meaningless to me.
    I would donate mine to charity...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,710
    Rep Power
    5

    Default Re: Error I don't understand

    Meh, rep. I only pass it around when I feel a post is epic enough that a like does not cover my appreciation for it.

    You know when I design my own forum software I'm going to take that social aspect to a new level. You'll have the following choices:

    - nod approvingly (+1)
    - pat on head (+2)
    - stomp shoulder (+3)
    - wedgie (+4)
    - propose marriage (+10)

    And on the flip side of that coin you'll be able to:

    - shake head (-1)
    - wag finger (-2)
    - distance yourself (-3)
    - call shame (-4)
    - ring the insane asylum (-10)

    That should get some heads turning.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    Join Date
    Mar 2014
    Posts
    72
    Rep Power
    0

    Default Re: Error I don't understand

    Quote Originally Posted by jim829 View Post
    Of course, considering both your rep points vs mine, I would benefit the most. Also, I don't think folks even understand the rep system (I'm not certain I understand it myself). They rep with no rep power. But its a nice gesture anyway.

    Regards,
    Jim
    Well if repping with 0 rep power does nothing, then you could always rep me. :)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error I don't understand

    rep points + $2.00 will buy you a cup of coffee.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Error I don't understand

    And from what I have seen in some cases, the coffee is normally priced at $1.80.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Error I don't understand

    I was including the tip.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. I dont understand this error, help please
    By Miller786 in forum New To Java
    Replies: 7
    Last Post: 02-01-2014, 06:31 PM
  2. I don't understand this error....
    By Lund01 in forum XML
    Replies: 13
    Last Post: 07-10-2011, 12:40 AM
  3. I do not understand this error with charAt()
    By AcousticBruce in forum New To Java
    Replies: 20
    Last Post: 12-25-2010, 06:19 PM
  4. I don't understand the error ... (help!)
    By XmisterIS in forum New To Java
    Replies: 3
    Last Post: 09-02-2010, 09:33 AM
  5. i don understand this error
    By Deon in forum New To Java
    Replies: 4
    Last Post: 01-12-2008, 10:03 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
  •