Mac上的java.lang.NullPointerException,但不在Windows上!

时间:2023-01-19 21:36:06

I have searched and searched for a solution to this problem, but it seems no-one else is experiencing it. Here's a description:

我已经搜索并搜索了这个问题的解决方案,但似乎没有其他人正在体验它。这是一个描述:

I'm creating a tile-based game with some co-programmers. We use a database class to do any and all loading of files and images. Everything works out just wonderfully until my Mac suddenly throws a NullPointerException at my face. The weird thing is it works on another dude's Mac (same model and everything) and it worked just fine on my computer a few minutes before the error. Nothing has been changed in the meantime.

我正在与一些联合编程人员一起创建基于磁贴的游戏。我们使用数据库类来执行任何文件和图像的加载。一切都很精彩,直到我的Mac突然在我脸上抛出NullPointerException。奇怪的是它适用于另一个家伙的Mac(相同型号和所有东西),并且在错误发生前几分钟它在我的计算机上运行得很好。在此期间没有任何改变。

This has happened before, and back then I re-installed the OS which sorted the problem, so it sounds like a tweak in either Eclipse (which I'm using as IDE) or MacOS. Anyone got any ideas?

这种情况以前发生过,然后我重新安装了对问题进行排序的操作系统,所以这听起来像是Eclipse(我用作IDE)或MacOS的调整。有人有任何想法吗?

The project can be found on: https://github.com/Gadamagaska/Quantum-Man

该项目可在以下网址找到:https://github.com/Gadamagaska/Quantum-Man

My stack-trace:

Exception in thread "main" java.lang.NullPointerException
    at entities.Level.getTile(Level.java:24)
    at database.Database.getTile(Database.java:206)
    at core.FoffyMain.drawBottomTiles(FoffyMain.java:82)
    at core.FoffyMain.draw(FoffyMain.java:64)
    at core.Core.gameLoop(Core.java:58)
    at core.Core.run(Core.java:34)
    at core.FoffyMain.main(FoffyMain.java:28)

2 个解决方案

#1


0  

you could have checks like this

你可以这样检查

public int getTile(int layer, int x, int y){
    if(layers!=null && layers.length > layer && layers[layer].length > y && layers[layer][y].length > x)
    {
        return layers[layer][y][x];
    }
    return -1;
}

or simple handle the null pointer exception

或者简单处理空指针异常

public int getTile(int layer, int x, int y) {
    try{

    return layers[layer][y][x];
    }
    catch(NullPointerException npe)
    {
        return -1;
    }
}

#2


0  

Are you sure this happens only on your Mac ?

你确定只在你的Mac上发生这种情况吗?

Looking at your code, it seems to me that your Level class is trying to access to the layer member before it is initialized.

查看代码,在我看来,您的Level类在初始化之前尝试访问图层成员。

Try to initialize layer in the constructor of Level instead of the addLayer method, I think it should resolve the problem, or at least a part of it.

尝试初始化Level的构造函数中的层而不是addLayer方法,我认为它应该解决问题,或至少解决它的一部分。

#1


0  

you could have checks like this

你可以这样检查

public int getTile(int layer, int x, int y){
    if(layers!=null && layers.length > layer && layers[layer].length > y && layers[layer][y].length > x)
    {
        return layers[layer][y][x];
    }
    return -1;
}

or simple handle the null pointer exception

或者简单处理空指针异常

public int getTile(int layer, int x, int y) {
    try{

    return layers[layer][y][x];
    }
    catch(NullPointerException npe)
    {
        return -1;
    }
}

#2


0  

Are you sure this happens only on your Mac ?

你确定只在你的Mac上发生这种情况吗?

Looking at your code, it seems to me that your Level class is trying to access to the layer member before it is initialized.

查看代码,在我看来,您的Level类在初始化之前尝试访问图层成员。

Try to initialize layer in the constructor of Level instead of the addLayer method, I think it should resolve the problem, or at least a part of it.

尝试初始化Level的构造函数中的层而不是addLayer方法,我认为它应该解决问题,或至少解决它的一部分。