Page 1 of 3 123 LastLast
Results 1 to 20 of 44
  1. #1
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Exclamation Variable Is Set, Works, But Then In Void, Returns 0

    The id variable is the problem
    Java Code:
    package com.cjburkey.games.boxee.objects;
    
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import com.cjburkey.games.boxee.GameState;
    import com.cjburkey.games.boxee.resources.Images;
    
    public class Block extends Rectangle {
    	
    	private static final long serialVersionUID = 1L;
    	
    	//--//
    	
    	public static final int blockSize = 64;
    	public static int id;
    	
    	public Block(int x, int y, int id) {
    		
    		setBounds(x, y, blockSize, blockSize);
    		Block.id = id;
    		System.out.println(id - 1 + "");
    		if(id == 4) {
    			setBounds(x, y + height / 2, blockSize, blockSize / 2);
    		}
    		
    	}
    	
    	public static void tick() { }
    	
    	public void draw(Graphics g) {
    		
    		if(!(Block.id <= 0)) {
    			g.drawImage(Images.blocks[Block.id - 1], x - (int) GameState.xOffset, y - (int) GameState.yOffset, width, height, null);
    		}
    		
    	}
    	
    }
    In the constructor, it returns corrent numbers, in the draw method, it returns 0. Why?
    Last edited by cjburkey01; 06-13-2014 at 11:27 PM. Reason: Missed important info

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I'm not certain what you mean. The draw method doesn't return anything.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    in the draw method, it returns 0.
    How do you know that?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I had it printing out, and the
    [code]
    Images.blocks[Block.id - 1] = picks the very first value every time
    [/code

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    If the value of id were 0, that code would have an invalid array index value.

    Can you make a small, complete program that compiles, executes and shows the problem?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    No, that's the thing, it only seem to happen with this, and it wasn't doing this before, I've changed nothing, and this started happening :(

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    If you are doing graphics, then perhaps your draw method is being called before Block.id is set. Because painting is typically done in different threads, asynchronously, the behavior is unpredictable.

    Also, why do you do this?
    Java Code:
     if(!(Block.id <= 0)) {
    instead of this?
    Java Code:
    if (Block.id > 0)
    They are equivalent expressions.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I've changed nothing, and this started happening
    Very unusual for programs to change behavior without something being changed.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I don't think I've changed anything, if you want I can post ALL my classes, or at least any that involve Block classes. Do you want me to?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    Have you tried debugging the code? Add a println() statement following all statements that change or refer to the id variable so you can see what its values are as it is changed and used.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    That's what I did, that's how I found out it was the right value until it hits the draw method, it is 0 in that method

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    You should have left all the println()s in the code and posted what was printed to show the evidence.
    Can you put ALL the println()s back in the code, execute the code and copy the print out here so we can see what is happening?
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    Ok, this is the entire thing, Constructor is the constructor, and DrawVoid is the public void draw method:
    Java Code:
    Success
    /Users/CJ/Boxee/maps/level1.map
    /Users/CJ/Boxee/maps/level2.map
    /Users/CJ/Boxee/maps/level1.map
    /Users/CJ/Boxee/maps/level2.map
    /Users/CJ/Boxee/maps/level1.map
    /Users/CJ/Boxee/maps/level2.map
    0
    Loading map: Level 1 - It's easy
    Size: 15x2
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: -1
    Constructor: 4
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    Constructor: 0
    DrawVoid: 0
    DrawVoid: 0
    DrawVoid: 0
    DrawVoid: 0
    (DrawVoid continues for about 5,000 lines, so I cut it off)
    After lookign through that, it does seem that the constructor is receive wierd numbers, but they are read from a file, and sent through the constructor:

    Java Code:
    package com.cjburkey.games.boxee.mapping;
    
    import java.awt.Graphics;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import javax.swing.JOptionPane;
    import com.cjburkey.games.boxee.objects.Block;
    
    public class Map {
    	
    	private String path, name;
    	private Block[] [] blocks;
    	private int width, height;
    	
    	public Map(String path) {
    		
    		this.path = path;
    		
    		loadMap(this.path);
    		
    	}
    	
    	public void draw(Graphics g) {
    		
    		for(int i = 0; i < blocks.length; i ++) {
    			
    			for(int j = 0; j < blocks[0].length; j ++) {
    				
    				blocks[i] [j].draw(g);
    				
    			}
    			
    		}
    		
    	}
    	
    	public void loadMap(String path) {
    		
    		InputStream is = null;
    		try {
    			is = new FileInputStream(path);
    		} catch (FileNotFoundException e1) {
    			e1.printStackTrace();
    		}
    		BufferedReader br = new BufferedReader(new InputStreamReader(is));
    		
    		try {
    			name = br.readLine();
    			width = Integer.parseInt(br.readLine());
    			height = Integer.parseInt(br.readLine());
    			
    			System.out.println("Loading map: " + name);
    			System.out.println("Size: " + width + "x" + height);
    			
    			blocks = new Block[height] [width];
    			
    			for(int y = 0; y < height; y ++) {
    				
    				String line = br.readLine();
    				
    				String[] tokens = line.split("\\s+");
    				
    				for(int x = 0; x < width; x ++) {
    					
    					blocks[y] [x] = new Block(x * Block.blockSize, y * Block.blockSize, Integer.parseInt(tokens[x]));
    					
    				}
    				
    			}
    		} catch (NumberFormatException | IOException e) {
    			JOptionPane.showMessageDialog(null, e.getStackTrace(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
    		}
    		
    		
    		
    	}
    	
    	public Block[] [] getBlocks() {
    		
    		return blocks;
    		
    	}
    	
    }
    and the file:
    Java Code:
    Level 1 - It's easy
    15
    2
    0 0 0 0 0 0 0 0 0 0 0 0 0 0 5
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    Last edited by cjburkey01; 06-14-2014 at 04:58 AM. Reason: TOO LONG!!! #2: Add file Code.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    The print out doesn't say where id was changed and where it was used. The output could say something like this where the suffix number is unique for each println() statement so the exact place of the println() that printed the line could be found:
    changed1 to 4
    used1 = 4
    used2 = 4
    changed2 to 5
    used1 = 5
    used2 = 5
    ...

    Where is the unexpected values in the print out?

    Also the code doesn't show the printlns that made the print out.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I don't understand

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    The print out has no meaning for me.
    Where was id given a value?
    Where was the value of id used?

    Where is the problem with the value of id? Where was its value unexpectedly changed?

    FYI There is no need to post hundreds of identical lines. Post a few then insert ... and then the last few.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I gave you the map reader, which reads the map file, returning the id's, it's the second to last code block, or:
    Java Code:
    package com.cjburkey.games.boxee.mapping;
     
    import java.awt.Graphics;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import javax.swing.JOptionPane;
    import com.cjburkey.games.boxee.objects.Block;
     
    public class Map {
         
        private String path, name;
        private Block[] [] blocks;
        private int width, height;
         
        public Map(String path) {
             
            this.path = path;
             
            loadMap(this.path);
             
        }
         
        public void draw(Graphics g) {
             
            for(int i = 0; i < blocks.length; i ++) {
                 
                for(int j = 0; j < blocks[0].length; j ++) {
                     
                    blocks[i] [j].draw(g);
                     
                }
                 
            }
             
        }
         
        public void loadMap(String path) {
             
            InputStream is = null;
            try {
                is = new FileInputStream(path);
            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
             
            try {
                name = br.readLine();
                width = Integer.parseInt(br.readLine());
                height = Integer.parseInt(br.readLine());
                 
                System.out.println("Loading map: " + name);
                System.out.println("Size: " + width + "x" + height);
                 
                blocks = new Block[height] [width];
                 
                for(int y = 0; y < height; y ++) {
                     
                    String line = br.readLine();
                     
                    String[] tokens = line.split("\\s+");
                     
                    for(int x = 0; x < width; x ++) {
                         
                        blocks[y] [x] = new Block(x * Block.blockSize, y * Block.blockSize, Integer.parseInt(tokens[x]));
                         
                    }
                     
                }
            } catch (NumberFormatException | IOException e) {
                JOptionPane.showMessageDialog(null, e.getStackTrace(), e.getMessage(), JOptionPane.ERROR_MESSAGE);
            }
             
             
             
        }
         
        public Block[] [] getBlocks() {
             
            return blocks;
             
        }
         
    }

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I'm still trying to understand where the unexpected value for id was.
    Where is id given a value and where is the value of id used?
    The debug print out should show that. An example print out that shows those two events:
    Java Code:
    id = 4;
    println("set1 = "+id) ;
    set1 = 4
    Java Code:
    arr[id];
    println("used1 = "+id) ;
    used1 = 4
    Java Code:
    arr[id];
    println("used2 = "+id) ;
    used2 = 0 <<<< Here the value was changed to 0 with out it being set to 0

    The debug print out needs to show where the value has changed without the code setting it!!!
    Last edited by Norm; 06-14-2014 at 05:34 AM.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    70
    Rep Power
    0

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    Oh, I get it, I'll post it in a sec

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Variable Is Set, Works, But Then In Void, Returns 0

    I'm done for tonight,
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 18
    Last Post: 03-26-2014, 11:43 PM
  2. Please explain static void and void methods are ?
    By shavindra in forum New To Java
    Replies: 5
    Last Post: 01-10-2013, 09:46 PM
  3. accessing variables in a method that returns a void
    By mochajava in forum New To Java
    Replies: 2
    Last Post: 02-12-2011, 09:00 PM
  4. Replies: 5
    Last Post: 01-25-2008, 12:53 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •