Java NullPointerException Collision 是来源,但不确定为什么

发布于 2024-12-22 10:44:35 字数 3946 浏览 3 评论 0原文

我尝试过运行代码而不会发生冲突,并且运行良好。但有了它,我得到了 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();
   }

}

I have tried running the code without the collision, and it works fine. But with it, I get a NullPointerException. The root of the problem has something to do with

blocked[xAxis][yAxis] = true;

edit: Sorry for not providing the traces earlier.

console:

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)

Properties of the 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>

full code.

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();
   }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-12-29 10:44:35

blocked 永远不会被创建。你需要在某个地方说:

blocked = new boolean[xsize][ysize];

xsize 和 ysize 被适当设置。

因为您还没有这样做,所以 blockednull 而不是数组。这就是为什么当您尝试将某些内容分配给数组中的元素时会出现异常的原因。

对于您的情况,您可以添加

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

init() 函数的顶部。

blocked is never created. You need to say somewhere:

blocked = new boolean[xsize][ysize];

where xsize and ysize are set appropriately.

Because you haven't done this, blocked is null instead of an array. That's why you're getting the exception when you try to assign something to an element in the array.

In your case, you could add

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

to the top of your init() function.

时光匆匆的小流年 2024-12-29 10:44:35

您没有初始化数组

private boolean[][] blocked;

,这就是为什么您在 blocked[xAxis][yAxis] = true; 中收到 NullPointerException 的原因

You didn't initialize the array

private boolean[][] blocked;

that's why you got a NullPointerException in blocked[xAxis][yAxis] = true;

骑趴 2024-12-29 10:44:35

我没有看到在任何地方初始化的阻塞二维数组,您需要将其初始化为地图的大小

I don't see the blocked 2D array initialized anywhere, you need to init it to the size of the map

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文