Results 1 to 17 of 17
  1. #1
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default [Help ERROR] RPG 2D Map!

    Okay so im trying to load a .txt to an array so it can be set a tiles. However on the loading im getting a "NullPointerException". Can someone point me into the right direction to where im going wrong? Possiably some source code please !
    Java Code:
    	private static final long serialVersionUID = 1L;
        public static Image GRASS;
        public static Image DIRT;
        
        private int x, y;
    	private static int xDirection;
    	private static int yDirection;
    	
    	static Rectangle[] blocks;
        static Image[] blockImg;
    	
    	private final int arrayNum = 523;
    	
        //Map navigation
        public static final int PAN_UP = 0, PAN_DOWN = 1, PAN_LEFT = 2, PAN_RIGHT = 3;
        
        public WorldMap() {
           
            ImageIcon g = new ImageIcon(System.getProperty("user.home") + "/Desktop/GAME/Map/grass.png");
            ImageIcon d = new ImageIcon(System.getProperty("user.home") + "/Desktop/GAME/Map/dirt.png");
    
            GRASS = g.getImage();
            DIRT = d.getImage();
            
            setSize(501, 419);
            
            blocks = new Rectangle[523];
            blockImg = new Image[523];
            
            loadMap();
           
        }
    
    	public void loadMap() {
    
            int x = 0;
            int y = 0;
            int j = 0;
            int loc = 0;
      
            try {
                File file = new File(System.getProperty("user.home") + "/Desktop/GAME/Map/map.txt");
                Scanner scan = new Scanner(file);
                for (String line = scan.nextLine(); line != null; line = scan.nextLine()) {
                    j = line.length() * 20;
                    while (j > x) {
                    	for(int i = 0; i < arrayNum; i++){
                    		char c = line.charAt(loc);
                    		if (c=='G') {
                    			blockImg[i] = GRASS;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    		if (c=='D') {
                    			blockImg[i] = DIRT;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    		x += 20;
                    		loc++;
                    	}
                    }
                    y += 20;
                    x = 0;
                    loc = 0;
                 }
            } catch (Exception e) {  
            }
        }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    However on the loading im getting a "NullPointerException"
    Could you copy and post the entire stack trace?

    Also, don't suppress any exceptions as they may be useful:

    Java Code:
    try {
        // etc
    } catch(Exception e) {
        e.printStackTrace();
    }

  3. #3
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    Ok its off of two files


    WorldMap.java
    Java Code:
    package org.map;
    
    import javax.swing.ImageIcon;
    import java.awt.*;
    import java.io.File;
    import java.util.Scanner;
    import javax.swing.JPanel;
    
    public class WorldMap extends JPanel{
    
    	private static final long serialVersionUID = 1L;
        public static Image GRASS;
        public static Image DIRT;
        
        private int x, y;
    	private static int xDirection;
    	private static int yDirection;
    	
    	static Rectangle[] blocks;
        static Image[] blockImg;
    	
    	private final int arrayNum = 523;
    	
        //Map navigation
        public static final int PAN_UP = 0, PAN_DOWN = 1, PAN_LEFT = 2, PAN_RIGHT = 3;
        
        public WorldMap() {
           
            ImageIcon g = new ImageIcon(System.getProperty("user.home") + "/Desktop/GAME/Map/grass.png");
            ImageIcon d = new ImageIcon(System.getProperty("user.home") + "/Desktop/GAME/Map/dirt.png");
    
            GRASS = g.getImage();
            DIRT = d.getImage();
            
            setSize(501, 419);
            
            blocks = new Rectangle[523];
            blockImg = new Image[523];
            
            loadMap();
           
        }
    
    	public void loadMap() {
    
            int x = 0;
            int y = 0;
            int j = 0;
            int loc = 0;
      
            try {
                File file = new File(System.getProperty("user.home") + "/Desktop/GAME/Map/map.txt");
                Scanner scan = new Scanner(file);
                for (String line = scan.nextLine(); line != null; line = scan.nextLine()) {
                    j = line.length() * 20;
                    while (j > x) {
                    	for(int i = 0; i < arrayNum; i++){
                    		char c = line.charAt(loc);
                    		if (c=='G') {
                    			blockImg[i] = GRASS;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    		if (c=='D') {
                    			blockImg[i] = DIRT;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    		x += 20;
                    		loc++;
                    	}
                    }
                    y += 20;
                    x = 0;
                    loc = 0;
                 }
            } catch (Exception e) {  
            }
        }
    	
    	public void drawMap(Graphics g) {
    		for(int z = 0; z < arrayNum; z++){
    			g.drawImage(blockImg[z], blocks[z].x, blocks[z].y, null);
            }
    	}
    	
    	
        public void moveMap(){
        	new WorldMap().x += xDirection;
        	new WorldMap().y += yDirection;
        }
        public static void stopMoveMap(){
            setXDirection(0);
            setYDirection(0);
        }
        private static void setXDirection(int dir){
            xDirection = dir;
        }
        private static void setYDirection(int dir){
            yDirection = dir;
        }
    	
    	public static void navigateMap(int nav){
            switch(nav){
                default:
                    System.out.println("default case entered... Doing nothing.");
                    break;
                case PAN_UP:
                    setYDirection(-1);
                    break;
                case PAN_DOWN:
                    setYDirection(1);
                    break;
                case PAN_LEFT:
                    setXDirection(-1);
                    break;
                case PAN_RIGHT:
                    setXDirection(1);
                    break;
            }
        }
    
    }

    Java Code:
    package org;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.MouseInfo;
    import java.awt.Point;
    import java.awt.PointerInfo;
    import java.awt.Rectangle;
    import java.awt.RenderingHints;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.RenderedImage;
    import java.io.File;
    import java.net.URL;
    
    import javax.imageio.ImageIO;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    import javax.swing.Timer;
    
    import org.map.WorldMap;
    import org.paint.TextBox;
    
    
    
    public class Board extends JPanel implements ActionListener, MouseListener, MouseMotionListener {
    
    	private static final long serialVersionUID = 1L;
    	private Timer timer;
        private Player player;
        private WorldMap map;
        static boolean collisionX = false;
        static boolean collisionY = false;
        static boolean collisionNEGX = false;
        static boolean collisionNEGY = false;
        int mouseX, mouseY;
        boolean DeveloperMode = true;
        int hpPercent = 100;
        
        
        //Paint
        public static String settingsPath =  System.getProperty("user.home") + "/Desktop/GAME/";
        public File settingsFolder = new File(settingsPath);
        Image chat = getImage("chatl.png", "http://img803.imageshack.us/img803/3681/chatl.png");
        Image healthPercent = getImage("healthoverlay.th.png", "http://img849.imageshack.us/img849/8724/healthoverlay.th.png");
        Image minimap = getImage("minimapv.png", "http://img641.imageshack.us/img641/8739/minimapv.png");
        Image inventory = getImage("inventorye.png", "http://img809.imageshack.us/img809/2364/inventorye.png");
        Image logout = getImage("logoutyn.png", "http://img832.imageshack.us/img832/4656/logoutyn.png");
        Image logoutOverMouse = getImage("logoutmouseover.png", "http://img716.imageshack.us/img716/7654/logoutmouseover.png");
        TextBox textBox = new TextBox(0, 572, 517, 28, TextBox.ColorScheme.WHITE, new Font("Arial", 0, 14));
        String message;
        Point p;
        boolean logoutMouse = false;
        private final Rectangle logoutBox = new Rectangle(673, 164, 70, 26);
        private final RenderingHints antialiasing = new RenderingHints(
                RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    
        private final Color color1 = new Color(0, 102, 0);
        private final Color color2 = new Color(0, 255, 51);
    
        private final Font font1 = new Font("Stone Sans ITC TT", 1, 10);
    
        
        public Board() {
            addKeyListener(new TAdapter());
            setFocusable(true);
            setDoubleBuffered(true);
            setOpaque(false);
            
            player = new Player();
            
            map = new WorldMap();
    
            timer = new Timer(5, this);
            timer.start();
            addMouseListener(this);
            addMouseMotionListener(this);
        }
    
    
    	public static Image getImage(String fileName, String imageURL) {
    		try {
    			File f = new File(settingsPath + File.separator + fileName);
    			if (!f.exists()) {
    				Image image = null;
    				URL url = new URL(imageURL);
    				image = ImageIO.read(url);
    				ImageIO.write((RenderedImage) image, "PNG", f);
    				return image;
    			}
    			Image img = ImageIO.read(f);
    			return img;
    		} catch (Exception e) {
    		}
    		return null;
    	}
        
        public void paint(Graphics g) {
            super.paint(g);
            Graphics2D g2d = (Graphics2D)g;
            g2d.setRenderingHints(antialiasing);
            g2d.drawImage(player.state(), player.getX(), player.getY(), this);
            g.drawImage(healthPercent, 700, 15, null);
            map.drawMap(g);
            g.setFont(font1);
            g.setColor(color1);
            g.drawString(player.getHP() + "%", 701, 35);
            g.setColor(color2);
            g.drawString(player.getHP() + "%", 699, 33);
            g.drawImage(chat, 0, 416, null);
            g.drawImage(minimap, 500, 0, null);
            g.drawImage(inventory, 500, 199, null);
            if (logoutMouse == false) {
            	g.drawImage(logout, 670, 160, null);
            } else {
            	g.drawImage(logoutOverMouse, 670, 160, null);
            }
            textBox.paint(g2d);
            if (DeveloperMode == true) {
            	g.setColor(Color.WHITE);
            	g.drawString("MousePosition: " + mouseX + ", " + mouseY, 5, 10);
            	g.drawString("Player Location:" + player.getX() + ", " + player.getY(), 5, 25);
            	g.drawString("collisionNEGX: " + collisionNEGX, 5, 40);
            	g.drawString("collisionX: " + collisionX, 5, 55);
            	g.drawString("collisionNEGY: " + collisionNEGY, 5, 70);
            	g.drawString("collisionY: " + collisionY, 5, 85);
            }
            Toolkit.getDefaultToolkit().sync();
            g.dispose();
        }
    
    
        public void checkBoundary() {
        	Rectangle user = player.getBounds();
        	if (user.getLocation().getX() < 0) {
        		collisionNEGX = true;
        	} else {
        		collisionNEGX = false;
        	}
        	if (user.getLocation().getX() > 475) {
        		collisionX = true;
        	} else {
        		collisionX = false;
        	}
        	if (user.getLocation().getY() < 0) {
        		collisionNEGY = true;
        	} else {
        		collisionNEGY = false;
        	}
        	if (user.getLocation().getY() > 384){
        		collisionY = true;
        	} else {
        		collisionY = false;
        	}
        }
        
    
    
    	public void actionPerformed(ActionEvent e) {
        	checkBoundary();
            player.move();
            repaint();  
        }
    
    
        private class TAdapter extends KeyAdapter {
    
            public void keyReleased(KeyEvent e) {
            	checkBoundary();
                player.keyReleased(e);
            }
    
            public void keyPressed(KeyEvent e) {
            	checkBoundary();
            	if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
                    textBox.removeLastChar();
            	} else if (e.getKeyChar() == KeyEvent.VK_ENTER) {
            		message = textBox.getText();
            	} else{
                    textBox.addChar(e.getKeyChar());
            	}
            	player.keyPressed(e);
            }
        }
    
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		p = e.getPoint();
    		if(logoutMouse == true && logoutBox.contains(p)) {
    			logoutMouse = false;
    		} else if(logoutMouse == false && logoutBox.contains(p)) {
    			logoutMouse = true;
    		}
    	}
    
    	@Override
        public void mouseEntered(MouseEvent e)
        {
    	}
    
    	@Override
    	public void mouseExited(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	@Override
    	public void mousePressed(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	@Override
    	public void mouseReleased(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	@Override
    	public void mouseDragged(MouseEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    
    	@Override
    	public void mouseMoved(MouseEvent e) {
    		PointerInfo a = MouseInfo.getPointerInfo();
    		Point point = new Point(a.getLocation());
    		SwingUtilities.convertPointFromScreen(point, e.getComponent());
    		mouseX=(int) point.getX();
    		mouseY=(int) point.getY();
    	}
    
    }
    IK its very messy but just want the job dones as right now ;P

  4. #4
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    ERROR:

    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at org.map.WorldMap.drawMap(WorldMap.java:84)
    	at org.Board.paint(Board.java:112)
    	at javax.swing.JComponent.paintChildren(JComponent.java:862)
    	at javax.swing.JComponent.paint(JComponent.java:1038)
    	at javax.swing.JComponent._paintImmediately(JComponent.java:5098)
    	at javax.swing.JComponent.paintImmediately(JComponent.java:4882)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:812)
    	at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
    	at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:694)
    	at javax.swing.RepaintManager.access$700(RepaintManager.java:41)
    	at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1672)
    	at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:677)
    	at java.awt.EventQueue.access$000(EventQueue.java:85)
    	at java.awt.EventQueue$1.run(EventQueue.java:638)
    	at java.awt.EventQueue$1.run(EventQueue.java:636)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:647)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    This is a double post: [Help ERROR] RPG 2D Map! (in the Java Gaming forum).

    Please don't do that as it makes communication very difficult when a conversation is happening in two places at once. Others may feel like I do that it's just not worth the effort to try and keep track of what is being said in multiple places.

    Decide which thread you mean to follow and post in the other one (with a link) saying so.

  6. #6
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    Sorry, was going to do that but for some strange reason the edit button is gone when in the Game part of the forums...

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    I've only just noticed that your original post was only a snippet of the original code (of the WorldMap class, I'm guessing).

    So which is line 84?

    -----

    Basically, somewhere on this line you are using a variable (or expression) which has a null value, but you are using it as if it had been given a nonnull value:

    Java Code:
    Thing thing;
    thing.foo(); // Bad! because thing is null
    int[] arr; 
    int bar = arr[0]; // Bad! because arr is null
    In such cases you have to look through your code to where you thought you had given the variable (or expression) a nonnull value and figure out why that didn't happen.

  8. #8
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    It's my draw map method and I thk it starts with the loader... I can't see any problem witht he draw method here it is

    Java Code:
    	public void drawMap(Graphics g) {
    		for(int z = 0; z < arrayNum; z++){
    			g.drawImage(blockImg[z], blocks[z].x, blocks[z].y, null);
            }
    	}

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    Yes, g.drawImage(... looks like where the problem arises.

    Did you understand what I said about the NullPointerException? The possible contenders for being null on this line are:

    g, blockImg and blocks[z]. You can get even more information by printing these variables:

    Java Code:
    public void drawMap(Graphics g) {
        System.out.println("Started drawMap(), g=" + g);
        System.out.println("    blockImg=" + blockImg);
        for(int z = 0; z < arrayNum; z++){
            System.out.println("    z=" + z + " blocks[z]=" + blocks[z]);
            g.drawImage(blockImg[z], blocks[z].x, blocks[z].y, null);
        }
    }
    Once you have figured out what is null when it shouldn't be, start looking for where you think it should have been given a nonnull value.

  10. #10
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    Okay. Ill try to find it. Hope I can with that :)

  11. #11
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    I think i have found it! what it's doing is looping the load method but doesn't know where to stop I believe. This is what I had gotten from your method

    Java Code:
    Started drawMap(), g=sun.java2d.SunGraphics2D[font=com.apple.laf.AquaFonts$DerivedUIResourceFont[family=Lucida Grande,name=Lucida Grande,style=plain,size=13],color=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0]]
        blockImg=[Ljava.awt.Image;@1d96f4b5
        z=0 blocks[z]=java.awt.Rectangle[x=0,y=0,width=20,height=20]
        z=1 blocks[z]=java.awt.Rectangle[x=20,y=0,width=20,height=20]
        z=2 blocks[z]=java.awt.Rectangle[x=40,y=0,width=20,height=20]
        z=3 blocks[z]=java.awt.Rectangle[x=60,y=0,width=20,height=20]
        z=4 blocks[z]=java.awt.Rectangle[x=80,y=0,width=20,height=20]
        z=5 blocks[z]=java.awt.Rectangle[x=100,y=0,width=20,height=20]
        z=6 blocks[z]=java.awt.Rectangle[x=120,y=0,width=20,height=20]
        z=7 blocks[z]=java.awt.Rectangle[x=140,y=0,width=20,height=20]
        z=8 blocks[z]=java.awt.Rectangle[x=160,y=0,width=20,height=20]
        z=9 blocks[z]=java.awt.Rectangle[x=180,y=0,width=20,height=20]
        z=10 blocks[z]=java.awt.Rectangle[x=200,y=0,width=20,height=20]
        z=11 blocks[z]=java.awt.Rectangle[x=220,y=0,width=20,height=20]
        z=12 blocks[z]=java.awt.Rectangle[x=240,y=0,width=20,height=20]
        z=13 blocks[z]=java.awt.Rectangle[x=260,y=0,width=20,height=20]
        z=14 blocks[z]=java.awt.Rectangle[x=280,y=0,width=20,height=20]
        z=15 blocks[z]=java.awt.Rectangle[x=300,y=0,width=20,height=20]
        z=16 blocks[z]=java.awt.Rectangle[x=320,y=0,width=20,height=20]
        z=17 blocks[z]=java.awt.Rectangle[x=340,y=0,width=20,height=20]
        z=18 blocks[z]=java.awt.Rectangle[x=360,y=0,width=20,height=20]
        z=19 blocks[z]=java.awt.Rectangle[x=380,y=0,width=20,height=20]
        z=20 blocks[z]=java.awt.Rectangle[x=400,y=0,width=20,height=20]
        z=21 blocks[z]=java.awt.Rectangle[x=420,y=0,width=20,height=20]
        z=22 blocks[z]=java.awt.Rectangle[x=440,y=0,width=20,height=20]
        z=23 blocks[z]=java.awt.Rectangle[x=460,y=0,width=20,height=20]
        z=24 blocks[z]=java.awt.Rectangle[x=480,y=0,width=20,height=20]
        z=25 blocks[z]=java.awt.Rectangle[x=500,y=0,width=20,height=20]
        z=26 blocks[z]=java.awt.Rectangle[x=520,y=0,width=20,height=20]
        z=27 blocks[z]=java.awt.Rectangle[x=540,y=0,width=20,height=20]
        z=28 blocks[z]=java.awt.Rectangle[x=560,y=0,width=20,height=20]
        z=29 blocks[z]=null
    See it seem to not 29+ and not loading the y value

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    The loop is not going to stop until it gets to arrayNum and you initialised that to a big number. Perhaps loadMap() should make sure it has the correct value? I am assuming that you mean it to record the number of tiles that were actually loaded.

    -----

    A separate issue, but why is it static? As a general rule don't make anything but main() static. (the PAN_XXX constants are a bit of an exception, and even they could be handled using an enum.)

  13. #13
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    Static becuase they were being used between class please tell me if i am doing this wrong and I am get close to figuring it out its some brackets out of place slightly :/

  14. #14
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    If an instance of some other class needs to know the value of arrayNum in some instance of WorldMap, then WorldMap should have a public method:

    Java Code:
    public int getArrayNum() {
        return arrayNum
    }
    Doing things this way means that instances of the other class cannot change the value of arrayNum. (which as you have seen can make the map fail). If instances of another class actually need to change the value of arrayNum then a setter method can be provided that allows that - the important thing here is that the map will know when this method is being called.

    -----

    Did you get the NullPointerException sorted out?

  15. #15
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    I didn't it seem that when I change the brackets around. The x and y get messed up :(. Its on the loader method if u see anything that seems wrong. What it is suppose to do is load the map from a given Source path. Then parse the file for the tile's image and location. Do you see any problem with the code for loadMap()?

    Java Code:
    public void loadMap() {
    
            int x = 0;
            int y = 0;
            int j = 0;
            int loc = 0;
      
            try {
                File file = new File(System.getProperty("user.home") + "/Desktop/GAME/Map/map.txt");
                Scanner scan = new Scanner(file);
                for (String line = scan.nextLine(); line != null; line = scan.nextLine()) {
                    j = line.length() * 25;
                    for(int i = 0; i < arrayNum; i++){
                    	while (j > x) {
                    		char c = line.charAt(loc);
                    		if (c=='G') {
                    			blockImg[i] = GRASS;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    		if (c=='D') {
                    			blockImg[i] = DIRT;
                    			blocks[i] =  new Rectangle(x, y, 20, 20);
                    		}
                    	x += 20;
                   		loc++;
                    }
                    y += 20;
                    x = 0;
                    loc = 0;
                 }
                }
            } catch (Exception e) {  
            }
        }

  16. #16
    santafan is offline Member
    Join Date
    Jun 2011
    Posts
    21
    Rep Power
    0

    Default Re: [Help ERROR] RPG 2D Map!

    Anyone? *Bump*

  17. #17
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: [Help ERROR] RPG 2D Map!

    I can't see anything obviously wrong with that method.

    But it's the value of arrayNum that is too large and causing the exception. What is that variable supposed to be? And why is it set to 523? (Are there 523 entires in the map.txt file?) If it is the number of tiles actually loaded (as I speculated above), then the loadMap() method should make sure it gets set to the correct number.

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 10:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 02:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 08:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 05:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 09:53 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
  •