Results 1 to 7 of 7
Thread: Error "variable is never read"
- 09-04-2010, 12:27 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Error "variable is never read"
I'm trying to perform a simple calculation based on some parameters. The problem is, I think I've defined xm and ym, btu the code doesn't like it. How do I get this to go forward to the calculation portion on the bottom?
Code:
public static void drawLines(){
Display myDisplay = new Display();
for (int cnt = 0; cnt < 4; cnt++){
for(int count = 0; count < 200; count++){
if (cnt == 0){
int xm = 1;
int ym = 0;
} else if (cnt == 1){
int xm = 0;
int ym = -1;
} else if (cnt == 2){
int xm = -1;
int ym = 0;
} else {
int xm = 0;
int ym = 1;
}
int x = count * xm + 100;
int y = count * ym + 100;
myDisplay.drawNextPixel(x,y);
}
}
}
Thanks
- 09-04-2010, 12:46 AM #2
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Sorry for the horrible way the code shows up. Actually the error says, "xm cannot be resolved to a variable". when I tell it to make one, it says the previous instances are not read. Same for ym.
Thanks.
- 09-04-2010, 02:28 AM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 12
you have declared xm and ym more than once
- 09-04-2010, 02:52 AM #4
Remember the scope rules for defining variables. Those defined inside {} are NOT known outside the {}
- 09-04-2010, 08:01 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-07-2010, 05:12 AM #6
Member
- Join Date
- Sep 2010
- Posts
- 3
- Rep Power
- 0
Thanks for all the help. I didn't realize it didn't carry through. I think it would have been simpler if it did. Wrote it a different way that got it to work. Is there a way that one could allow the variables to cross those boundaries?
- 09-07-2010, 05:30 AM #7
Senior Member
- Join Date
- Jun 2010
- Location
- Destiny Islands
- Posts
- 690
- Rep Power
- 0
Java Code:int a = 5; if (true) { int b = 6; a = 6; // a == 6, b == 6 } // a == 6, b is an error
The reason it does not follow the "simpler" method that you describe is that, in case that if block does not execute, then there would be no variable and the VM would not know what to do.
Similar Threads
-
Question about error "Exception in thread "main" java.lang.NoSuchMethodError: main
By ferdzz in forum New To JavaReplies: 5Last Post: 06-22-2010, 03:51 PM -
Runtime error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
By shantimudigonda in forum New To JavaReplies: 1Last Post: 11-20-2009, 07:58 PM -
Error "can not find symbol variable"
By FullMetalHollow in forum New To JavaReplies: 5Last Post: 10-04-2009, 09:51 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM
Bookmarks