gpt4 book ai didi

java - 数组赋值会产生 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 13:35:18 24 4
gpt4 key购买 nike

我在为数组赋值时遇到问题,该数组之前是这样声明的:

Enemy[] enemies = new Enemy[100];

我还有另一个变量定义生成的敌人数量:

int numEnemies = 0;

后来,当触摸屏幕时,我(尝试)动态将此数组的成员分配给 Enemy 对象:

public void spawnEnemy() {
Enemy enemy = new Enemy(...);
... initialise enemy ...
enemies[numEnemies++] = enemy; // this line causes NPE
}

我有这个堆栈跟踪:

01-24 02:00:28.509: E/AndroidRuntime(1394): FATAL EXCEPTION: UpdateThread
01-24 02:00:28.509: E/AndroidRuntime(1394): Process: com.example.menutest, PID: 1394
01-24 02:00:28.509: E/AndroidRuntime(1394): java.lang.NullPointerException
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.GameScene.spawnEnemy(GameScene.java:333)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.GameScene.initJoints(GameScene.java:325)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.GameScene.loadLevel(GameScene.java:189)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.GameScene.createScene(GameScene.java:80)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.BaseScene.<init>(BaseScene.java:24)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.GameScene.<init>(GameScene.java:32)
01-24 02:00:28.509: E/AndroidRuntime(1394): at com.example.menutest.SceneManager$1.onTimePassed(SceneManager.java:118)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.handler.timer.TimerHandler.onUpdate(TimerHandler.java:94)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.handler.UpdateHandlerList.onUpdate(UpdateHandlerList.java:47)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.Engine.onUpdateUpdateHandlers(Engine.java:618)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.Engine.onUpdate(Engine.java:605)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.Engine.onTickUpdate(Engine.java:568)
01-24 02:00:28.509: E/AndroidRuntime(1394): at org.andengine.engine.Engine$UpdateThread.run(Engine.java:858)

编辑:

我的应用程序是用 AndEngine 编写的,因此从 SceneManager 调用 CreateScene() 方法,我确信它工作得很好。

public class GameScene extends BaseScene {

Enemy enemies[] = new Enemy[100];
int numEnemies = 0;

public void createScene() {
ResourcesManager.getInstance().setGameScene(this);
this.setOnSceneTouchListener((IOnSceneTouchListener) ResourcesManager.mBaseGameActivity);
createBackground();
createHUD();
createPhysicsWorld();
// the above set up scene for later use
loadLevel(1);
}



public void loadLevel(int levelID) {
initJoints();
}



private void initJoints() {
mPlanet = new Planet(400, 225, ResourcesManager.getInstance().planet, mVertexBufferObjectManager, 500);
mPlanet.mBody = PhysicsFactory.createBoxBody(mPhysicsWorld, mPlanet, BodyType.StaticBody, FIXTURE_DEF);
mPlanet.mBody.setUserData(mPlanet);
this.attachChild(mPlanet);
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(mPlanet, mPlanet.mBody, true, true));

float x1 = 200;
float y1 = 112;
mSatellite = SatelliteFactory.createSatellite(SatelliteType.BASIC, x1, y1, mVertexBufferObjectManager, mPlanet);
this.attachChild(mSatellite);

spawnEnemy(); // causes exception

}



private void spawnEnemy() {
float y = getRandomY();
float maxHealth = 100;
Enemy enemy = new Enemy(0, y, ResourcesManager.getInstance().satellite_no_image, ResourcesManager.mVertexBufferObjectManager, maxHealth);
enemy.mBody = PhysicsFactory.createBoxBody(mPhysicsWorld, enemy, BodyType.DynamicBody, FIXTURE_DEF);

this.attachChild(enemy);
enemy.mBody.setLinearVelocity(new Vector2((mPlanet.getX() - enemy.mBody.getPosition().x) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT,
(mPlanet.getY() - enemy.mBody.getPosition().y) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT));
enemy.mBody.setLinearVelocity(new Vector2(mPlanet.getX(), mPlanet.getY() - y));
mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(enemy, enemy.mBody, true, true));

enemies[numEnemies++] = enemy; // NPE
}
}

最佳答案

请添加 Log.d() 并查看显示的内容:

Log.d("~~~","\n\n\n\n~~~ enemies="+enemies+"  enemy="+enemy);
enemies[numEnemies++] = enemy; // this line causes NPE

我怀疑这是因为初始化的顺序,该函数肯定是从构造函数或初始化程序调用的。

另请注意日志中的以下内容:

at com.example.menutest.GameScene.initJoints(GameScene.java:325)
at com.example.menutest.GameScene.loadLevel(GameScene.java:189)
at com.example.menutest.GameScene.createScene(GameScene.java:80)
at com.example.menutest.BaseScene.<init>(BaseScene.java:24)
at com.example.menutest.GameScene.<init>(GameScene.java:32)

GameScene 构造函数调用 BaseScene 构造函数,而 BaseScene 构造函数又调用 GameScene 方法 - 在 GameScene 实例创建完成之前。

此外,您写道:当触摸屏幕时,我(尝试)动态将此数组的成员分配给 Enemy 对象,但我看到代码在超时时启动:

at com.example.menutest.SceneManager$1.onTimePassed(SceneManager.java:118)

关于java - 数组赋值会产生 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21326476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com