Null pointer help

hey guys, not sure if this is the correct section so i apologise in advance, but im having some problems getting my game started.

I am using a tile map and at the moment all i want to display on the screen is the contents of my map text file, the code compiles fine, i run it and it switches to full screen and exits almost straight away. I then get a scary null pointer which looks a bit like this:

java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2759)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2749)
at Game.draw(Game.java:28)
at GameCore.gameLoop(GameCore.java:72)
at GameCore.run(GameCore.java:29)
at Game.main(Game.java:10)

i believe it doesnt like something i have in a class ive called game so i will include that to see if thats where the problem lies:

[quote]import java.awt.*;

public class Game extends GameCore {

TileMap map;

public static void main(String[] args) {

	Game a = new Game();
	a.run();
	
}

public void init() {

super.init();
map = new TileMap(2, 2);
map.loadMap("map.txt");


}


public void draw (Graphics2D g) {

	for (int y = 0; y <= map.getHeight(); y++){
		for (int x = 0; x <= map.getWidth(); x++){
			g.drawImage(map.getTileImage(x,y), x*40, y*40, null);
			
			}
	}

}

}
[/quote]
any help would be very appreciated :wink:
(if any other information is needed let me know )

I’d guess at “map.getTileImage(x,y)” returning null, might help to see the other class (TileMap)

Kev

okay here is my tilemap class

import java.awt.*;
import java.util.*;
import javax.swing.ImageIcon;
import java.io.*;

public class TileMap
{
private Image [][] tiles;
private char [] [] chartiles;
private LinkedList sprites;

private Image crowdImg,boardsImg;

// create a new tile map with specified height and width
public TileMap (int width, int height)
 {
 tiles  = new Image [width] [height];
 sprites  = new LinkedList();
 
  crowdImg = new ImageIcon("images/crowd.png").getImage();
  boardsImg = new ImageIcon("images/board.png").getImage();
  
  }
 
  // gets the width of the tile map
  public int getWidth()
  {
  return tiles.length;
  }
  
  // get the height of the tilemap
  
  public int getHeight()
   {
   return tiles[0].length;
   }
   
   // get a tile at a specfied location.
   public Image getTile(int x, int y)
   {
   if (x<0|| x>= getWidth()|| y<0 || y>= getHeight())
   {
   return null;
   }
   else {
   return tiles [x] [y];
    }
  }
  
  // sets the tile at a specified location
  public void setTile(int x,int y, Image tile)
  {
  tiles[x][y] = tile;
 }
 
 

 
 // read in the slighty scary tilemap file
 public void loadMap(String filename) {
 
    ArrayList lines = new ArrayList();
    int width = 0;
    int height = 0;
    
    try
    {
    //read every line
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    while (true){
       String line = reader.readLine();
       //no more lines to read
       if (line==null){
          reader.close();
          break;
       }
       // add every line except for comments
       if(!line.startsWith("#")) {
          lines.add(line);
          width = Math.max(width,line.length());
       }
    }
    
    height = lines.size();
    tiles = new Image [width][height];
    chartiles = new char [width][height];
    
    for (int y=0; y<height; y++)
    {
       String line = (String)lines.get(y);
       for (int x=0; x<line.length(); x++) {
          char ch = line.charAt(x);
          
          chartiles[x][y] = ch;
          if (ch == 'c') tiles[x][y] = crowdImg;
          if (ch== 'b') tiles[x][y] = boardsImg;
        }
    }
    }
    catch (Exception e)
    {
       System.err.println("Failed to open tile map " + e);
    }
}          
          
          
public Image getTileImage(int x, int y) {
   return tiles[x][y];
}

public char getTileChar(int x, int y) {
   return chartiles[x][y];
}

 }// end of the TileMap class 

Yes, KevGlass’ suggestion seems likely.

AFAI can see, you only assign an image to a point in the 2D array when some character is ‘b’ or ‘c’. Make sure that all the tiles are non-null, e.g. by filling out all the spaces of the array with a black image or something, so you can easily debug.

Also, your getTile method may return null if the requested indices are out of bounds. Maybe you should just make it throw ArrayIndexOutOfBoundsException, since that will cover the rest of the possible ways the null-pointer exception could occur.

okay very silly error was causing the last problem, it was searching for ‘b’ and ‘c’ in the map but i had ‘B’ and ‘C’ ;D

however it is now indeed throwing an array index out of bound error

java.lang.ArrayIndexOutOfBoundsException: 135
at TileMap.getTileImage(TileMap.java:108)
at Game.draw(Game.java:28)
at GameCore.gameLoop(GameCore.java:72)
at GameCore.run(GameCore.java:29)
at Game.main(Game.java:10)
Exception in thread “main” Exit code: 1

what does this suggest ive done wrong? its trying to return something that is out of bounds?

for (int x=0; x<line.length(); x++) {

Hmm… what if line.length() != getWidth()

hmm yeah it really didnt like that, failed to load map and threw in a null pointer for good measure :frowning:

Failed to open tile map java.lang.StringIndexOutOfBoundsException: String index out of range: 0
java.lang.NullPointerException
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:50)
at sun.java2d.pipe.DrawImage.copyImage(DrawImage.java:736)
at sun.java2d.pipe.ValidatePipe.copyImage(ValidatePipe.java:147)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2759)
at sun.java2d.SunGraphics2D.drawImage(SunGraphics2D.java:2749)
at Game.draw(Game.java:28)
at GameCore.gameLoop(GameCore.java:72)
at GameCore.run(GameCore.java:29)
at Game.main(Game.java:10)
Exception in thread “main” Exit code: 1
There were errors

okay sorted it now

was a silly mistake with my map itself

tried a different one and it worked fine

big thanks to all those who offered some input, nice to see a non elitist attitude when people ask for help…unlike some other places ;D

Thats what this topic is for, askign for help!

Actually thats about half of the purpose of EVERY topic here.

You may find people ocassionally give you short answers becuase their time is limited, or even ocassionally act annoyed if you havent checked yet what they consider basic reources, but in general the community here is pretty reliably helpful ;D