Results 1 to 7 of 7
  1. #1
    Phitur is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default 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

  2. #2
    Phitur is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    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.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    you have declared xm and ym more than once

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

    Default

    Remember the scope rules for defining variables. Those defined inside {} are NOT known outside the {}

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

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    you have declared xm and ym more than once
    ... in several separate blocks (scopes); inside those blocks those variables are never read.

    kind regards,

    Jos

  6. #6
    Phitur is offline Member
    Join Date
    Sep 2010
    Posts
    3
    Rep Power
    0

    Default

    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?

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Java Code:
    int a = 5;
    if (true) {
        int b = 6;
        a = 6;
        // a == 6, b == 6
    }
    // a == 6, b is an error
    The first line in the above code is representative of how you would accomplish this task. If it wasn't an integer, you could use null or another initializer value to assign to the value.

    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

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  3. Error "can not find symbol variable"
    By FullMetalHollow in forum New To Java
    Replies: 5
    Last Post: 10-04-2009, 09:51 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 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
  •