I'm trying out my first attempt at designing a world via a text file. It's not going so well.
Most of my code is copied from a friend's code, I'm trying to reverse engineer it, but I'm getting a nullpointer exception in a place that I didn't expect.
Here is the code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.Scanner;
import java.util.List;
/**
* Write a description of class TEST here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class TEST extends World
{
private static final int TILE_WIDTH = 50;
private static final int TILE_HEIGHT = 50;
private int leftX = TILE_WIDTH / 2;
private int topY = TILE_HEIGHT / 2;
private static final int MAP_HEIGHT = 4;
private static final int MAP_WIDTH = 9;
private List<Actor> list;
/**
* Constructor for objects of class TEST.
*
*/
public TEST()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1, false);
readMapFile();
}
public void readMapFile()
{
InputStream istream = getClass().getResourceAsStream("a.txt");
String textmap = new String;
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(istream)));
for(int i = 0; i < textmap.length; i++)
{
while(s.hasNextLine())
{
textmap = s.nextLine();
}
}
s.close();
addTiles(textmap);
}
private void addTiles(String THEMAP)
{
for(int x = 0; x < THEMAP.length; x++)
{
addColumn(x, THEMAP);
}
}
private void addColumn(int x, String THEMAP)
{
int tileX = leftX + TILE_WIDTH * x;
String column = THEMAP;
for(int y = 0; y < column.length(); y++) //the error is occuring right here.
{
int tileY = topY + TILE_HEIGHT * y;
char type = column.charAt(y);
if(type == 'T')
{
addObject(new Tile(1), tileX, tileY);
}
if(type == 'F')
{
addObject(new Tile(2), tileX, tileY);
}
if(type == 'R')
{
addObject(new Tile(3), tileX, tileY);
}
if(type == 'r')
{
addObject(new Tile(4), tileX, tileY);
}
}
}
}
I'm stumped. I can't figure out what the compiler is having trouble finding. It makes a reference to one other class, here is the code for that class:
public class Tile extends Actor
{
public void act()
{
}
public Tile()
{
setImage("tile0.png");
}
public Tile(int x)
{
setImage("tile" + x + ".png");
}
}
And lastly, there are just four images in the images folder. tile1.png through tile4.png.
I just need to know what the compiler is having trouble finding. Thanks!

