#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0

    Nullpoint exception with Arraylist method


    Problem: When i add new arrayline to Arraylist i get nullpoint exception
    Whats my goal: Every time getTile array matches with colorcode it adds the x location to Arraylist.

    [spoiler=codes]
    GrassTile.java
    Code:
    /* Tile Grass
     * Tyypiline muru kus saab kõndida aga mingit erilist boonust ei anna.
     */
    package Tiles;
    
    import java.util.ArrayList;
    
    import Graafika.Pilt;
    import Graafika.Screen;
    
    public class GrassTile extends Tile {
    	public ArrayList <Integer> coordinates = new ArrayList <Integer>();
    	
    	public GrassTile() {
    	}
    
    	public void update(){		
    		Tile.clock();
    	}
    
    	public void render (int x, int y, Screen screen) {
    		screen.renderTile(x<<4, y<<4, Pilt.grass);
    		}
    	}
    }

    Level.java

    Code:
    package Map;
    
    import EpoxGame.MouseHandler;
    import Graafika.Screen;
    import Tiles.Tile;
    import Tiles.GrassTile;
    
    public class Level {
    
    		protected int width, height;
    		protected int[] tilesInt;
    		public static int[] tiles;
    		private GrassTile grassy;		
    				
    		public Level (int width, int height) {
    			this.width = width;
    			this.height = height;
    			tilesInt = new int[width*height];
    			generateLevel();
    	}
    
    		public Level(String path) {
    			loadLevel(path);
    			generateLevel();
    		}
    		
    		protected void generateLevel() {
    		}
    		
    		protected void loadLevel(String path){
    		
    		}
    		
    		@SuppressWarnings("unused")
    		private void time() {
    			
    		}
    		
    		public void render(int xScroll, int yScroll, Screen screen) {
    			screen.setOffset(xScroll, yScroll);
    			int x0 = xScroll>>4; //>>4 = 16 , sest 4x4=16
    			int x1 = (xScroll + screen.width+16)>>4;
    			int y0 = yScroll>>4;
    			int y1 = (yScroll + screen.height+16)>>4;
    			
    			for (int y=y0; y<y1; y++){
    				for (int x=x0; x<x1; x++){
    					getTile(x, y).render(x, y, screen);
    				}
    			}
    		}
    
    		// Grass = 0xff 00ff0c
    		// Water = 0xff 005aff
    		// Rock = 0xff 7e817f
    		public Tile getTile(int x, int y){
    			if (x<0 || y<0 || x>= width || y>= height) return Tile.voidtile;
    
    			if (tiles[x+y*width] == 0xff00ff0c)	{
    				grassy.coordinates.add(x);
    				return Tile.grass;
    			}
    			if (tiles[x+y*width] == 0xff7e817f) return Tile.stone;
    			if (tiles[x+y*width] == 0xff005aff) return Tile.water;
    			return Tile.voidtile;
    		}
    }
    [/spoiler]
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Note that the problem isn't adding to the ArrayList. It's that grassy is null.

    Code:
    private GrassTile grassy;
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by bullet
    Note that the problem isn't adding to the ArrayList. It's that grassy is null.

    Code:
    private GrassTile grassy;
    ty, problem solved.
    lol. so missed that.

IMN logo majestic logo threadwatch logo seochat tools logo