Help with tile loader code

Last night i started work on a tile loader/map creator piece of code its no where near finished at the mo, but im having a small problem with it it doesnt seem to like this piece of the code

map = createImage(sizeX*32, sizeY*32);

mapGraphics = map.getGraphics();

any ideas why? it compiles ok, but when it runs it crashes with an NullPointerException for the mapGraphics = map.getGraphics(); line

also out of curisosity is there an easier/quicker way to copy part of an image and store it into another image than the one im using?

any thoughts would be greatly appreciated

import java.awt.*;

import java.applet.*;

import java.awt.image.*;

import java.net.*;

import java.io.*;

class LevelMapextends Applet{

Graphics mapGraphics;

Image map;

Image[] tiles=new Image[50];

int sizeX;

int sizeY;

publicvoid init(){

sizeX=0;

sizeY=0;

}

publicvoid setTiles (int pos, Image tile){

tiles[pos]=tile;

}

publicvoid createImageMap (String contents){

for (int i=0 ; i<contents.length() ; i++){

if(contents.charAt(i)=='\n'){

sizeY++;

}

else{

sizeX++;

}

}

map = createImage(sizeX*32, sizeY*32);

mapGraphics = map.getGraphics();

}

publicvoid paint(Graphics g, platformMain parent){

for (int i=0 ; i><3 ; i++){

g.drawImage(tiles[i],32*i,0,parent);

}

for (int i=0 ; i<3 ; i++){

g.drawImage(tiles[i+3],32*i,32, parent);

}

}

}

publicclass platformMainextends Appletimplements Runnable{

MediaTracker tracker;

Thread painter;

Graphics bufferGraphics;

Image offscreen;

Dimension dim;

LevelMap level1=new LevelMap();

Graphics tileGraphics;

Image tile;

int delay;

Image tileSet_1;

publicvoid init(){

dim=getSize();

offscreen = createImage(dim.width,dim.height);

bufferGraphics = offscreen.getGraphics();

tile = createImage(32,32);

tileGraphics = tile.getGraphics();

int fps = 35;

delay = (fps > 0) ? (1000 / fps) : 100;

level1.init();

tracker =new MediaTracker(this);

tileSet_1=getImage(getCodeBase(),"Images/tile1.png");

tracker.addImage(tileSet_1, 0);

try{

tracker.waitForID(0);

}catch (InterruptedException e){

}

//level 1 setup

for (int i=0 ; i<(tileSet_1.getWidth(this)-1)/33 ; i++){

level1.setTiles(i, splitTileSet(tileSet_1, i, 32, 32, null,true));

}

level1.createImageMap(loadMap("test.txt"));

repaint();

}

/**********************

CUSTOM CLASSES

***********************/

public Image splitTileSet(Image map,int pos,int width,int height, Color transparent,boolean faded){

tileGraphics.drawImage(map, 0-((pos*width)+(pos+1)), -1,this);

int[] pixels=newint[width*height];

PixelGrabber pg=new PixelGrabber(tile,0,0,width,height,pixels,0,width);

try{

pg.grabPixels();

}catch(InterruptedException e){}

return createImage(new MemoryImageSource(width,height,pixels,0,width));

}

public String loadMap (String path){

String whole="";

String line;

StringBuffer buf;

URL url=null;

try{

url =new URL (getCodeBase(), path );

}catch (MalformedURLException e){}

try{

InputStream in=url.openStream();

BufferedReader dis =new BufferedReader(new InputStreamReader(in));

buf =new StringBuffer () ;

while ((line = dis.readLine()) !=null){

whole+=line+"\n";

}

in.close();

}catch (IOException e ){}

return whole;

}

/**********************

REQUIRED CLASSES

***********************/

publicvoid paint(Graphics g){

level1.paint(bufferGraphics,this);

bufferGraphics.setColor(Color.black);

bufferGraphics.drawString("testing...testing",10,10);

g.drawImage(offscreen,0,0,this);

}

publicvoid update(Graphics g){

paint(g);

}

publicvoid start(){

if(painter==null){

painter=new Thread(this);

painter.start();

}

}

publicvoid run(){

long tm = System.currentTimeMillis();

while (Thread.currentThread() == painter){

try{

tm += delay;

Thread.sleep(Math.max(0, tm - System.currentTimeMillis()));

}catch (InterruptedException e){

break;

}

repaint();

}

}

publicvoid stop(){

if(painter!=null){

painter=null;

}

}

}

[9900 byte] By [ptom98a] at [2007-10-2 15:38:03]
# 1
Look at the documentation for the return value of createImage(int, int). It explicitly states that it will return null instead of an Image in some circumstances.You might want to have a look at the BufferedImage class.
DaanSa at 2007-7-13 15:18:19 > top of Java-index,Other Topics,Java Game Development...