Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: error while creating a "clear" method

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Grumpy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default error while creating a "clear" method

    Hello!
    I was following this(a video on youtube called "Game Programming: Episode 10- Clearing the Screen" by "TheChernoProject ) tutorial on how to make a game when an error occurred.

    in my clear method in the screen class, it shows an error in this line;

    pixels[i] = 0;

    the "i" says that it cannot be resolved to a variable. why is this?

    i have checked and i have the same coda as "Cherno" has in the video, but i still get this error.

    --- Update ---

    This video;

    https://www.youtube.com/watch?v=Gt6WvnQu7R8


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: error while creating a "clear" method

    Can you post an SSCCE demonstrating what you're trying?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Grumpy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: error while creating a "clear" method

    so basically i have followed a series on how to make a game. in a game you have to render to a screen and you must clear the screen before every new render. I have come thus far that i have made a method that will clear the screen, but i get an error in that clear method. The error concerns an "i". I can use that "i" in the lines of code just above, butt not in this line.

    this is my clear method;

    public void clear(){
    for (int i = 0; i < pixels.length; i++);
    pixels [ i ] = 0;
    }

    its the "i" in the square brackets after calling pixels.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: error while creating a "clear" method

    The semi-colon at the end of the 'for' statement ends the 'for' clause. The 'i' in the next line is now an orphan, having never been declared. Remove the semi-colon at the end of the 'for' statement.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: error while creating a "clear" method

    Take a look at your for loop. Notice how it ends in a semicolon, which means that you do nothing in each iteration of the loop, and then exit that loop. After you exit the loop, you try to set pixels[i], but since you're outside the loop, i doesn't exist anymore! I think you want this instead:

    public void clear(){
       for (int i = 0; i < pixels.length; i++){
          pixels [ i ] = 0;&#65279;
       }
    }

    Watch out for similar problems with if statements!
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Grumpy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: error while creating a "clear" method

    Whoops, Tanks!
    now i get an error while debugging the code, it reads;

    Save Problems

    Save could not be completed. Try File > Save As... if the problem persists.

    Reason:
    some characters cannot be mapped using "Cp1252" character encoding.
    Either change the encoding or remove the characters which are not supported by "Cp1252" character encoding.


    (English is not my main language)
    What could cause this?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: error while creating a "clear" method

    Again, you need to post an SSCCE that demonstrates the error. Do you really get that error while running a debugger? What debugger are you using? What editor? Have you tried following the advice in the error itself?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: error while creating a "clear" method

    (English is not my main language)
    What could cause this?
    You were born in a non-English speaking country.

    Sorry, couldn't resist.

  9. #9
    Junior Member
    Join Date
    Jan 2014
    Posts
    8
    My Mood
    Grumpy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: error while creating a "clear" method

    Fixed it, i just start from the beginning and copied the code from the old files and it worked like a charm!
    Thanks for the help!

    Btw I an using Eclipse as my ide.

Similar Threads

  1. Creating a "repeator" method.
    By Jergetson in forum Java Theory & Questions
    Replies: 4
    Last Post: December 8th, 2013, 12:11 PM
  2. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  3. "cannot find symbol" error when trying to use the getInt() method.
    By simpson_121919 in forum Collections and Generics
    Replies: 6
    Last Post: February 21st, 2013, 12:48 PM
  4. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  5. Replies: 7
    Last Post: August 13th, 2011, 01:22 AM