gpt4 book ai didi

java - 线程中出现异常 "main"Java.lang.nullPointerException 错误?

转载 作者:行者123 更新时间:2023-12-02 07:58:07 24 4
gpt4 key购买 nike

我收到以下错误:

Exception in thread "main" java.lang.NullPointerException
at BallContainerImage.update(BallContainerImage.java:101)
at BallContainer.addBall(BallContainer.java:93)
at Game.ejectBall(Game.java:92)
at LotteryTestB.main(LotteryTestB.java:19)

第 19 行包含:

      dramaticGame1.ejectBall();

Dramatic Game 类包含以下内容:

public class DramaticMachine extends Machine
{
// Constructor is given the person's name.
public DramaticMachine(String name, int length)
{
super(name, length);
}


public Ball ejectBall()
{
if (getNoOfBalls() >= 0)
return null;
else
{
//Math.random() * getNoOfBalls yields a number
//which is >=0 and < number of balls.
int ejectedBallIndex = (int) (Math.random() * getNoOfBalls());

for (int selectedBallIndex = 0; selectedBallIndex < ejectedBallIndex; selectedBallIndex++)
{
Ball selectedBall = getBall(selectedBallIndex);
selectedBall.flash(4, 5);
}

Ball ejectedBall = getBall(ejectedBallIndex);
ejectedBall.flash(4, 5);

swapBalls(ejectedBallIndex, getNoOfBalls() -1);
removeBall();

return ejectedBall;


}//else
}//ejectBall


public String getType()
{
return "Dramatic Lottery Machine";
}//getType
}//dramaticMachine

我该如何解决这个问题?

这是 DramaticGame 类的代码:

public class DramaticGame extends Game
{
// Constructor is given the person's name.
public DramaticGame(String machineName, int machineSize, String rackName, int
rackSize)
{
super(machineName,machineSize,rackName,rackSize);

}

public Machine makeMachine(String machineName, int machineSize)
{
return new DramaticMachine(machineName, machineSize);
}//makeMachine
}

这是 LotteryTestB 的代码:

public class LotteryTestB
{
public static void main (String args[])
{
SpeedController speedController
= new SpeedController(SpeedController.HALF_SPEED);
LotteryGUI gui = new LotteryGUI("TV Studio", speedController);

Worker worker = new TraineeWorker("Jim",0);

DramaticGame dramaticGame1 = new DramaticGame("Lott O'Luck Larry", 49,
"Slippery's Mile", 7);
gui.addGame(dramaticGame1);

worker.fillMachine(dramaticGame1);

for (int count = 1; count <=dramaticGame1.getRackSize(); count++)
{
dramaticGame1.ejectBall();
speedController.delay(40);
}//for

}//main
}//LotteryTestB

最佳答案

NullPointerException 是更容易追查的问题之一。这意味着某些引用未正确初始化。通过使用调试器单步调试代码应该很容易弄清楚。

如果您无法使用调试器,堆栈跟踪可以让您轻松做到这一点。只有四个地方可以看,并且它准确地说明了它们的位置。

at BallContainerImage.update(BallContainerImage.java:101)
at BallContainer.addBall(BallContainer.java:93)
at Game.ejectBall(Game.java:92)
at LotteryTestB.main(LotteryTestB.java:19)

不是底部的一个。对dramaticGame 的引用是该行中唯一的引用,您可以调用new 来初始化它。继续下一篇。添加一条日志或打印语句来证明空引用在哪里,然后去正确初始化它。

我认为您的代码分层不正确。除非您可以将问题分解为更小的 block ,对它们进行单元测试直到它们起作用,然后使用该代码构建复杂的解决方案,否则您永远无法实现此目的。

将用户界面与游戏本身分开。让游戏正常运行,然后担心显示问题。

关于java - 线程中出现异常 "main"Java.lang.nullPointerException 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350068/

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