Java NullPointerException碰撞是源,但不确定为什么

时间:2011-12-22 04:40:20

标签: java nullpointerexception tiles pixels slick2d

我尝试在没有碰撞的情况下运行代码,它运行正常。但有了它,我得到一个NullPointerException。问题的根源与

有关
blocked[xAxis][yAxis] = true;

编辑:很抱歉没有提供早期的跟踪。

控制台:

Wed Dec 21 20:32:52 PST 2011 INFO:Slick Build #274
Wed Dec 21 20:32:52 PST 2011 INFO:LWJGL Version: 2.8.2
Wed Dec 21 20:32:52 PST 2011 INFO:OriginalDisplayMode: 1440 x 900 x 32 @60Hz
Wed Dec 21 20:32:52 PST 2011 INFO:TargetDisplayMode: 800 x 600 x 0 @0Hz
Wed Dec 21 20:32:53 PST 2011 INFO:Starting display 800x600
Wed Dec 21 20:32:53 PST 2011 INFO:Use Java PNG Loader = true
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
Wed Dec 21 20:32:54 PST 2011 INFO:Found 0 controllers
Exception in thread "main" java.lang.NullPointerException
at RPG.Main.init(Main.java:41)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at RPG.Main.main(Main.java:102)

TiledMap的属性

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE map SYSTEM "http://mapeditor.org/dtd/1.0/map.dtd">
<map version="1.0" orientation="orthogonal" width="10" height="10" tilewidth="32"                   <tileheight="32">
<tileset name="wall" firstgid="1" tilewidth="32" tileheight="32">
<image source="wall.png"/>
<tile id="0">
<properties>
<property name="blocked" value="true"/>
</properties>
</tile>
</tileset>
<tileset name="grass" firstgid="2" tilewidth="32" tileheight="32">
<image source="grass.png"/>
</tileset>
<layer name="Layer 0" width="10" height="10">
<data encoding="base64" compression="gzip">
H4sIAAAAAAAAAGNkYGBgJBIzEcCUqBvsYtTwLzEYAHHwHBmQAQAA
</data>
</layer>
</map>

完整代码。

public class Main extends BasicGame {

Image player = null;
Image land = null;

private float playerX = 32f;
private float playerY = 32f;
private static final int SIZE = 32;

private boolean[][] blocked;

private TiledMap map;


public Main()
{
    super("Classic");
}

@Override
public void init(GameContainer gc)
        throws SlickException 
{
    player = new Image("RPG/player.png");
    map = new TiledMap("RPG/Map.tmx");

    for (int xAxis=0;xAxis<map.getWidth(); xAxis++)
    {
         for (int yAxis=0;yAxis<map.getHeight(); yAxis++)
         {
             int tileID = map.getTileId(xAxis, yAxis, 0);
             String value = map.getTileProperty(tileID, "blocked", "false");
             if ("true".equals(value))
             {
                 blocked[xAxis][yAxis] = true;
             }
         }
     }

}

@Override
public void update(GameContainer gc, int delta)
        throws SlickException
{
    Input input = gc.getInput();

    if (input.isKeyDown(Input.KEY_LEFT)) {
        if (!isBlocked(playerX, playerY - delta * 0.1f))
        {
        playerX-= delta * 0.1f;
        }
    }
    if (input.isKeyDown(Input.KEY_RIGHT)) {
        if (!isBlocked(playerX, playerY - delta * 0.1f))
        {
        playerX+= delta * 0.1f;
        }
    }
    if (input.isKeyDown(Input.KEY_UP)) {
        if (!isBlocked(playerX, playerY - delta * 0.1f))
        {
        playerY-= delta * 0.1f;
        }
    }
    if (input.isKeyDown(Input.KEY_DOWN)) {
        if (!isBlocked(playerX, playerY - delta * 0.1f))
        {
        playerY+= delta * 0.1f;
        }
    }

}

public void render(GameContainer gc, Graphics g)
        throws SlickException
{
    map.render(0, 0);

    player.draw(playerX, playerY);

}

private boolean isBlocked(float playerX, float playerY)
{
    int xBlock = (int)playerX / SIZE;
    int yBlock = (int)playerY / SIZE;
    return blocked[xBlock][yBlock];
}

public static void main(String[] args)
        throws SlickException
{
     AppGameContainer app = new AppGameContainer( new Main() );
     app.setDisplayMode(800, 600, false);
     app.start();
   }

}

3 个答案:

答案 0 :(得分:2)

永远不会创建

blocked。你需要在某个地方说:

blocked = new boolean[xsize][ysize];

正确设置xsize和ysize。

由于您尚未执行此操作,blockednull而非数组。这就是为什么当你尝试为数组中的元素赋值时,你会得到异常。

在您的情况下,您可以添加

blocked = new boolean[map.getWidth()][map.getHeight()];

init()功能的顶部。

答案 1 :(得分:0)

您没有初始化数组

private boolean[][] blocked;

这就是您在NullPointerException

中获得blocked[xAxis][yAxis] = true;的原因

答案 2 :(得分:0)

我没有看到被阻止的2D数组在任何地方初始化,你需要将其初始化为地图的大小