Results 1 to 7 of 7
Thread: Parameter help/question
- 01-07-2012, 03:54 AM #1
Parameter help/question
Hi, I have a quick question and need help with a certain issue with parameters..
Now I still consider myself learning and getting used to programming, and I don't quite understand the great use/function of parameters.. and when I use it in this program I'm working on, I get a certain error. I'm following a few lines of code from this website and it shows this:
I'm only cutting out a bit of the whole program because this is the only issue I'm having. Two void methods I'm using is setX(int x) and setY(int y). But when I try to use them in other methods, I get an error saying "x cannot be resolved as a variable" and "y cannot be resolved as a variable". Is there any reason why it's saying this? Because the website I'm using wrote it exactly like that and it works for them, apparently. I'd also like to know a solution to this problem without changing the variables for the 3 methods after SetX/setY. One solution I had was changing the variables for those three methods from x/y to xPos/yPos (field variable used in the program) which generally works with the program, but as I said, I'm trying to learn how to use parameters outside of their methods so I want to find a solution sticking with x and y.Java Code:public void setX(int x) { this.xPos = x; } public void setY(int y) { this.yPos = y; } public void draw (Graphics2D g2) { if (useImage == false) { g2.setColor(c); g2.fill(new Ellipse2D.Double(xPos, yPos, ballWidth, ballHeight)); } else { g2.drawImage(getImage(), x, y, null); } } public void move() { this.x += 1; this.y += 2; } public void move(int xDir, int yDir) { this.x += xDir; this.y += yDir; }
- 01-07-2012, 01:41 PM #2
Re: Parameter help/question
How are you compiling the program?when I try to use them in other methods, I get an error
Please post the full text of the error message. You edited version is leaving out information.
Here is a sample:
Java Code:TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
- 01-07-2012, 01:51 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Parameter help/question
The only peculiarity I see is in line #21: where do the variables x and y come from?
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-07-2012, 03:26 PM #4
Re: Parameter help/question
I simply press the Compile button at the top.. I'm getting 6 errors, all of them that say the same thing. "x cannot be resolved as a variable" and "y cannot be resolved as a variable" on lines 21, 28, 29, 34, and 35.
The variables x and y come from lines 1 and 8. They're from parameters from those two methods.
-
Re: Parameter help/question
- 01-07-2012, 03:32 PM #6
Re: Parameter help/question
You need to look at the concept of "scope" for the definitions of variables. The definition of a variable in a program is limited to be within the enclosing {}s. Outside of the enclosing {}s the variable is NOT defined.
The variable defined on line 1 is NOT defined past line 4. It is out of scope hence is not known, on line 21
- 01-07-2012, 03:36 PM #7
Re: Parameter help/question
I was thinking that too, I just wanted to make sure there was absolutely no way to use those variables outside of their methods. Alright then, now that I know it's true, it must be a mistake in the code. I'll simply go back to replacing those variables with xPos and yPos. Thanks for the help.
Similar Threads
-
Ireport x parameter SQL
By Reginaldo in forum Advanced JavaReplies: 0Last Post: 10-21-2011, 02:03 PM -
how to pass parameter from one jsp to another jsp
By kader_sit in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 06-10-2010, 02:04 PM -
Using ArrayList as a parameter
By Crypts in forum New To JavaReplies: 6Last Post: 05-13-2010, 06:56 AM -
Can a method take itself as parameter?
By bukake in forum New To JavaReplies: 10Last Post: 09-06-2008, 09:26 PM -
Retrieving a parameter through URL.
By hisouka in forum Java ServletReplies: 1Last Post: 09-06-2008, 12:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks